My AC Java Solution, with explanation


  • 8
    W
    public int countDigitOne(int n) {
        if(n <= 0){
            return 0;
        }
        int iCount = 0;
        long iFactor = 1;
        long iLowerNum = 0;
        int iCurrNum = 0;
        long iHigherNum = 0;
        while (n / iFactor != 0){
            iLowerNum = n % iFactor;
            iCurrNum = (int)(n / iFactor) % 10;
            iHigherNum = n / (iFactor * 10);
            switch (iCurrNum){
                case 0:
                    iCount += iHigherNum * iFactor;
                    break;
                case 1:
                    iCount += iHigherNum * iFactor + iLowerNum + 1;
                    break;
                default:
                    iCount += (iHigherNum + 1) * iFactor;
                    break;
            }
            iFactor *= 10;
        }
        return iCount;
    }
    

    about explanation,you can see here:http://blog.csdn.net/u013027996/article/details/17126977
    thx!


  • 0
    J

    这个网站应该是已经被中国人攻陷了


  • 0
    L

    对,我在国内也在刷呢!!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.