#Leetcode# 233. Number of Digit One
阿新 • • 發佈:2019-04-13
NPU long long pub put input 代碼 pre 做人 public
https://leetcode.com/problems/number-of-digit-one/
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.
Example:
Input: 13 Output: 6 Explanation: Digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.
代碼:
class Solution { public: int countDigitOne(int n) { int res = 0; long long a = 1, b = 1; while (n) { res += (n + 8) / 10 * a + (n % 10 == 1) * b; b += n % 10 * a; a *= 10; n /= 10; } return res; } };
暴力超時 新的一天從被數論教做人開始
#Leetcode# 233. Number of Digit One