题解 | #整数中1出现的次数#
整数中1出现的次数(从1到n整数中1出现的次数)
https://www.nowcoder.com/practice/bd7f978302044eee894445e244c7eee6
import java.util.*; public class Solution { public int NumberOf1Between1AndN_Solution1(int n) { int count=0; for(int cursor=0;cursor<=n;cursor++){ int tmp=cursor; while(tmp!=0){ if(tmp%10==1){ count++; } tmp/=10; } } return count; } public int NumberOf1Between1AndN_Solution(int n){ int count=0; for(int cursor=0;cursor<=n;cursor++){ String tmp=String.valueOf(cursor); char[] chars=tmp.toCharArray(); for(char c:chars){ if(c=='1') count++; } } return count; } }