File tree 2 files changed +27
-0
lines changed
2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change 204
204
229|[ Majority Element II] ( ./0229-majority-element-ii.js ) |Medium|
205
205
231|[ Power of Two] ( ./0231-power-of-two.js ) |Easy|
206
206
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|
207
208
234|[ Palindrome Linked List] ( ./0234-palindrome-linked-list.js ) |Easy|
208
209
235|[ Lowest Common Ancestor of a Binary Search Tree] ( ./0235-lowest-common-ancestor-of-a-binary-search-tree.js ) |Easy|
209
210
236|[ Lowest Common Ancestor of a Binary Tree] ( ./0236-lowest-common-ancestor-of-a-binary-tree.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments