[LeetCode][JavaScript]Number of Digit One

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.

For example:
Given n = 13,
Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.



数学题,真是为难了数学拙计的我了。

递归分治,拿8192举栗子:

把8192拆成:

1-999 -> 递归(999)

1000-1999 -> 1000个1 + 递归(999)

2001-2999 -> 递归(999)

.

.

8000-8192 -> 递归(192)

总数是:递归(999)*8 + 1000 + 递归(192)

要注意到如果是1192

总数是:递归(999)*1 + (1000 - 192 + 1) + 递归(192)

(1000 - 192 + 1)是1000-1192中千位上的1。

 1 /**
 2  * @param {number} n
 3  * @return {number}
 4  */
 5 var countDigitOne = function(n) {
 6     if(n <= 0){
 7         return 0;
 8     }else if(n < 10){
 9         return 1;
10     }
11     var len = n.toString().length;
12     var base = Math.pow(10, len - 1);
13     var answer = parseInt(n / base);
14     var remainder = n % base;
15     var oneInBase = 0;
16     if(answer === 1){
17         oneInBase = n - base + 1;
18     }else{
19         oneInBase = base;
20     }
21     return countDigitOne(base - 1) * answer + oneInBase + countDigitOne(remainder);
22 };

然后少开几个变量,强行把代码写在一行上,在实际项目中不要这么做哦,过段时间自己也读不懂了。

 1 /**
 2  * @param {number} n
 3  * @return {number}
 4  */
 5 var countDigitOne = function(n) {
 6     if(n <= 0) return 0;
 7     if(n < 10) return 1;
 8     var base = Math.pow(10, n.toString().length - 1);
 9     var answer = parseInt(n / base);
10     return countDigitOne(base - 1) * answer + (answer === 1 ? (n - base + 1) : base) + countDigitOne(n % base);
11 };
时间: 2024-12-28 11:30:29

[LeetCode][JavaScript]Number of Digit One的相关文章

LeetCode 233. 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. For example:Given n = 13,Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 本来想凑一个关于阶乘的题目,做个笔记,不枉我昨天A八个

Java for LeetCode 233 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. For example: Given n = 13, Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 解题思路: 递归 static public in

(*medium)LeetCode 233.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. For example:Given n = 13,Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 代码如下: public class Solution

[LeetCode]67. Number of Digit One1的个数和

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example:Given n = 13,Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. Hint: Beware of overflow. S

[LeetCode]233 Number of Digit One(数位DP)

题目链接:https://leetcode.com/problems/number-of-digit-one/?tab=Description 题意:求[1,n]内1的个数. 裸数位DP,好久不写都快不会写了..这么水 1 class Solution { 2 public: 3 int dp[30][30]; 4 int digit[30]; 5 6 int dfs(int l, int cnt, bool flag) { 7 if(l == 0) return cnt; 8 if(!flag

[LeetCode][JavaScript]Number of Islands

Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are

[LeetCode][JavaScript]Number of 1 Bits

https://leetcode.com/problems/number-of-1-bits/ Number of 1 Bits Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11' has binary representat

[LeetCode] Number of Digit One 数字1的个数

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example: Given n = 13, Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. Hint: Beware of overflow.

Leetcode: 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. For example: Given n = 13, Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. Hint: Beware of overflow.