求出任意非负整数区间中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;
    }
}

 

posted @ 2019-05-15 20:52  紫色的雪  阅读(337)  评论(0编辑  收藏  举报