求出任意非负整数区间中1出现的次数(从1 到 n 中1出现的次数)。

public class Solution {
    
    public int NumberOf1Between1AndN_Solution(int n) {
        if(n==0){
            return 0;
        }
        int count=0;
        for(int i = 1;i<=n;i++){
            String str = Integer.toString(i);
            char[] chars = str.toCharArray();
            for(int j=0;j<chars.length;j++){
                if(chars[j]=='1'){
                    count++;
                }
            }
        }
        return count;
    }
}

猜你喜欢

转载自www.cnblogs.com/q-1993/p/10872111.html