首页 > 其他 > 详细

Leetcode-5010 Digit Count in Range(范围内的数字计数)

时间:2019-06-02 10:10:59      阅读:284      评论:0      收藏:0      [点我收藏+]
 1 class Solution
 2 {
 3     public:
 4         int count(int n, int x)
 5         {
 6             int cnt = 0, k;
 7             for (int i = 1; k = n / i; i *= 10)
 8             {
 9                 int high = k / 10;
10                 if (x == 0)
11                 {
12                     if (high)
13                     {
14                         high--;
15                     }
16                     else
17                     {
18                         break;
19                     }
20                 }
21                 cnt += high * i;
22                 int cur = k % 10;
23                 if (cur > x)
24                 {
25                     cnt += i;
26                 }
27                 else if (cur == x)
28                 {
29                     cnt += n - k * i + 1;
30                 }
31             }
32             return cnt;
33         }
34         int digitsCount(int d, int low, int high)
35         {
36             return count(high,d)-count(low-1,d);
37         }
38 };

参考:http://www.it610.com/article/4964533.htm

Leetcode-5010 Digit Count in Range(范围内的数字计数)

原文:https://www.cnblogs.com/Asurudo/p/10961900.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!