leetcode 日经贴,Cpp code -Number of Digit One

原创 2015年07月09日 20:59:53

Number of Digit One


class Solution {
public:
    int countDigitOne(int n) {
        if (n <= 0) return 0;
        vector<int> vi;
        while (n) {
            vi.push_back(n % 10);
            n /= 10;
        }
        reverse(vi.begin(), vi.end());
        int a = 0, b = 0, lenvi = vi.size();
        int ret = 0;
        for (int i = 0; i < lenvi; ++i) {
            int powerb = 1;
            b = 0;
            for (int j = i + 1; j < lenvi; ++j) {
                powerb *= 10;
                b = b * 10 + vi[j];
            }
            if (vi[i] > 1) {
                ret += (a + 1) * powerb;
            } else if (vi[i] == 1) {
                ret += a * powerb + b + 1;
            } else {
                ret += a * powerb;
            }
            a = a * 10 + vi[i];
        }
        return ret;
    }
};


class Solution {
public:
    int countDigitOne(int n) {
        if (n <= 0) {
            return 0;
        }
        int count1s = 0;
        vector<int> digits;
        while (n) {
            digits.push_back(n % 10);
            n /= 10;
        }
        int sz = digits.size();
        vector<int> pow10(sz), lowval(sz);
        pow10[0] = 1;
        lowval[0] = 0;
        for (int i = 1; i < digits.size(); ++i) {
            pow10[i] = pow10[i - 1] * 10;
            lowval[i] = lowval[i - 1] + digits[i - 1] * pow10[i - 1];
        }
        int highval = 0;
        for (int i = sz - 1; i >= 0; --i) {
            if (digits[i] > 1) {
                count1s += (highval + 1) * pow10[i];
            } else if (digits[i] == 1) {
                count1s += highval * pow10[i] + lowval[i] + 1;
            } else {
                count1s += highval * pow10[i];
            }
            highval = highval * 10 + digits[i];
        }
        return count1s;
    }
};


 

LeetCode No.233 Number of Digit One

LeetCode No.233 Number of Digit One

leetcode解题笔记:Number of Digit One

233.Number of Digit One Given an integer n, count the total number of digit 1 appearing in all non...

[LeetCode]233.Number of Digit One

题目Given an integer n, count the total number of digit 1 appearing in all non-negative integers less ...

Leetcode Number of Digit One

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th...

[分析总结:leetcode-Number of Digit One]寻找整数1到n之间所有数字中1出现的次数

leetcode原题链接:https://leetcode.com/problems/number-of-digit-one/ 题目描述: 要求寻找整数1到n之间所有数字中,1出现的次数。如给定...

leetcode -- Number of Digit One -- 数学题,找规律

https://leetcode.com/problems/number-of-digit-one/参考http://www.cnblogs.com/grandyang/p/4629032.htmlh...
  • xyqzki
  • xyqzki
  • 2015年12月16日 17:11
  • 269

LeetCode-233.Number of Digit One

https://leetcode.com/problems/number-of-digit-one/ Given an integer n, count the total number o...

【LeetCode-Hard-2】【Number of Digit One】【1~N中‘1’出现次数】

第二题,中等难度,看到正确率略小于0.2就做了 233 Number of Digit One 19.5% Medium Given an integer n, count ...
  • hjy0125
  • hjy0125
  • 2015年07月21日 22:37
  • 335

LeetCode Algorithms 233. Number of Digit One 题解

题目:Given an integer n, count the total number of digit 1 appearing in all non-negative integers less...

[LeetCode]Number of Digit One,解题报告

题目Given an integer n, count the total number of digit 1 appearing in all non-negative integers less ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode 日经贴,Cpp code -Number of Digit One
举报原因:
原因补充:

(最多只允许输入30个字)