#Leetcode# 233. Number of Digit One
https://leetcode.com/problems/number-of-digit-one/
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
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; } };
暴力超时 新的一天从被数论教做人开始

更多精彩