Skip to content

Commit 0f07030

Browse files
committed
Add solution #233
1 parent 11d4804 commit 0f07030

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@
204204
229|[Majority Element II](./0229-majority-element-ii.js)|Medium|
205205
231|[Power of Two](./0231-power-of-two.js)|Easy|
206206
232|[Implement Queue using Stacks](./0232-implement-queue-using-stacks.js)|Easy|
207+
233|[Number of Digit One](./0233-number-of-digit-one.js)|Hard|
207208
234|[Palindrome Linked List](./0234-palindrome-linked-list.js)|Easy|
208209
235|[Lowest Common Ancestor of a Binary Search Tree](./0235-lowest-common-ancestor-of-a-binary-search-tree.js)|Easy|
209210
236|[Lowest Common Ancestor of a Binary Tree](./0236-lowest-common-ancestor-of-a-binary-tree.js)|Medium|

solutions/0233-number-of-digit-one.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 233. Number of Digit One
3+
* https://leetcode.com/problems/number-of-digit-one/
4+
* Difficulty: Hard
5+
*
6+
* Given an integer n, count the total number of digit 1 appearing in all non-negative
7+
* integers less than or equal to n.
8+
*/
9+
10+
/**
11+
* @param {number} n
12+
* @return {number}
13+
*/
14+
var countDigitOne = function(n) {
15+
if (n <= 0) {
16+
return 0;
17+
} else if (n < 10) {
18+
return 1;
19+
}
20+
21+
const base = 10 ** (n.toString().length - 1);
22+
const answer = parseInt(n / base);
23+
24+
return countDigitOne(base - 1) * answer
25+
+ (answer === 1 ? (n - base + 1) : base) + countDigitOne(n % base);
26+
};

0 commit comments

Comments
 (0)