File tree 2 files changed +34
-0
lines changed
2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 320
320
399|[ Evaluate Division] ( ./0399-evaluate-division.js ) |Medium|
321
321
400|[ Nth Digit] ( ./0400-nth-digit.js ) |Medium|
322
322
401|[ Binary Watch] ( ./0401-binary-watch.js ) |Easy|
323
+ 402|[ Remove K Digits] ( ./0402-remove-k-digits.js ) |Medium|
323
324
404|[ Sum of Left Leaves] ( ./0404-sum-of-left-leaves.js ) |Easy|
324
325
405|[ Convert a Number to Hexadecimal] ( ./0405-convert-a-number-to-hexadecimal.js ) |Easy|
325
326
407|[ Trapping Rain Water II] ( ./0407-trapping-rain-water-ii.js ) |Hard|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 402. Remove K Digits
3
+ * https://leetcode.com/problems/remove-k-digits/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given string num representing a non-negative integer num, and an integer k, return
7
+ * the smallest possible integer after removing k digits from num.
8
+ */
9
+
10
+ /**
11
+ * @param {string } num
12
+ * @param {number } k
13
+ * @return {string }
14
+ */
15
+ var removeKdigits = function ( num , k ) {
16
+ const stack = [ ] ;
17
+
18
+ for ( const n of num ) {
19
+ while ( k > 0 && stack . length && stack [ stack . length - 1 ] > n ) {
20
+ stack . pop ( ) ;
21
+ k -- ;
22
+ }
23
+ stack . push ( n ) ;
24
+ }
25
+
26
+ while ( k > 0 ) {
27
+ stack . pop ( ) ;
28
+ k -- ;
29
+ }
30
+
31
+ const result = stack . join ( '' ) . replace ( / ^ 0 + / , '' ) ;
32
+ return result . length ? result : '0' ;
33
+ } ;
You can’t perform that action at this time.
0 commit comments