1. 程式人生 > >[Swift]LeetCode233. 數字1的個數 | Number of Digit One

[Swift]LeetCode233. 數字1的個數 | Number of Digit One

The example nbsp UNC git solution val following int

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 }

[Swift]LeetCode233. 數字1的個數 | Number of Digit One