描述
输入一个整数 n ,求1~n这n个整数的十进制表示中1出现的次数
例如,1~13中包含1的数字有1、10、11、12、13因此共出现6次
public class Solution {
public int NumberOf1Between1AndN_Solution(int n) {
int num = 0;
for(int i = 1; i <= n; i++){
String strNum = Integer.toString(i);
for(int j = 0; j < strNum.length(); j++){
if(strNum.charAt(j) == '1'){
num += 1;
}
}
}
return num;
}
}
京公网安备 11010502036488号