为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode233. 数字1的个数 | Number of Digit One

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10204847.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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.

给定一个整数 n,计算所有小于等于 n 的非负整数中数字 1 出现的个数。

示例:

输入: 13
输出: 6 
解释: 数字 1 出现在以下数字中: 1, 10, 11, 12, 13 。

8ms
 1 class Solution {
 2     func countDigitOne(_ n: Int) -> Int {
 3         var     n = n 
 4         var  rest = 0
 5         var  base = 1
 6         var count = 0
 7         
 8         while n > 0 {
 9             let digit = n % 10
10             n = n/10
11             count += n * base
12             switch digit {
13                 case 0: ()
14                 case 1: count += rest + 1
15                 default: count += base
16             }
17             rest = digit * base + rest
18             base *= 10
19         }
20         return count
21     }
22 }

8ms

 1 class Solution {
 2     func countDigitOne(_ n: Int) -> Int {
 3         var num = n
 4         var res = 0
 5         var multiply = 1
 6         var remainder = 0
 7         while num > 0 {
 8             let val = num % 10
 9             
10             if val <= 1 {
11                 res += (num / 10) * multiply
12                 if val == 1 {
13                     res += remainder + 1
14                 }
15             }
16             else {
17                 res += (num / 10 + 1) * multiply
18             }
19             
20             multiply *= 10
21             remainder = n % multiply
22             num /= 10
23         }
24         
25         return res
26     }
27 }

12ms

 1 class Solution {
 2     func countDigitOne(_ n: Int) -> Int {
 3         if n <= 0 { return 0 }
 4         
 5         let digitCount = Int(log2(Double(n))/log2(10))+1
 6         var count = 0
 7         for i in 0..<digitCount {
 8             let base = Int(pow(10, Double(i)))
 9             let nextBase = base * 10
10             let currentDigit = n/base % 10
11             
12             count += (n/nextBase) * base
13             
14             if currentDigit == 0 {
15                 count += 0
16             } else if currentDigit == 1 {
17                 count += n % base + 1
18             } else {
19                 count += base
20             }
21         }
22         return count
23     }
24 }

36ms

 1 class Solution {
 2     func countDigitOne(_ n: Int) -> Int {
 3         if n <= 0 {
 4             return 0
 5         }
 6         
 7         if n < 10 {
 8             return 1
 9         }
10      
11         var m = n
12         var l = 0
13         var c = 1
14         while m != 0 {
15             l = m % 10
16             m /= 10
17             c *= 10
18         }
19         c /= 10
20         let k = l * c
21         
22         if l == 1 {
23             return n - k + 1 + countDigitOne(n-k) + countDigitOne(c-1)
24         }else {
25             return countDigitOne(2 * c - 1) + (l - 2)*countDigitOne(c-1) + countDigitOne(n-k)
26         }
27     }
28 }

 

posted @ 2019-01-01 15:02  为敢技术  阅读(299)  评论(0编辑  收藏  举报