求出1~13的整數中1出現的次數,並算出100~1300的整數中1出現的次數

//方案一:code

public class Solution {io

public int NumberOf1Between1AndN_Solution(int n) {
int sum=0;
int temp=0;
    for(int i=0;i<=n;i++){
        
           temp=i;
        
        while(temp!=0){
            if(temp%10==1)
                sum++;
              temp=temp/10;
        }
        
    }
    
    return sum;
}

}class

//方案二:while

public class Solution {co

public int NumberOf1Between1AndN_Solution(int n) {
int sum=0;

    while(n>0){
        String s= String.valueOf(n);
        char c[]=s.toCharArray();
        
        for(int i=0;i<c.length;i++)
            if(c[i]=='1')
                sum++;
        n--;
    }
    
    return sum;
}

}return

相關文章
相關標籤/搜索