File tree Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change 1313 - Day 10: [ Find Town Judge] ( https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-ten/index.ts ) :poop :
1414 - Day 11: [ Flood Fill] ( https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-eleven/index.ts ) :raising_hand :
1515 - Day 12: [ Single Element in a Sorted Array] ( https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-twelve/index.ts ) :hear_no_evil :
16+ - Day 13: [ Remove K Digits] ( https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-thirteen/index.ts ) :tongue :
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } num
3+ * @param {number } k
4+ * @return {string }
5+ */
6+ export const removeKdigits = function ( num : string , k ) {
7+ if ( num . length === k ) return '0' ;
8+ // 堆疊解法
9+ let i : number = 0 ;
10+ let stack : string [ ] = [ ] ;
11+ while ( i < num . length ) {
12+ while ( k > 0 && stack . length && stack [ stack . length - 1 ] > num [ i ] ) {
13+ stack . pop ( ) ;
14+ k -- ;
15+ }
16+ stack . push ( num [ i ] ) ;
17+ i ++ ;
18+ }
19+ stack = k > 0 ? stack . slice ( 0 , - k ) : stack ;
20+ return stack . join ( '' ) . replace ( / ^ 0 + / , '' ) || '0' ;
21+ } ;
22+
23+ const a = removeKdigits ( '0354' , 1 ) ;
24+ const b = removeKdigits ( '10200' , 1 ) ;
25+ const c = removeKdigits ( '112' , 1 ) ;
26+ console . log ( 'a' , a ) ;
27+ console . log ( 'b' , b ) ;
28+ console . log ( 'c' , c ) ;
You can’t perform that action at this time.
0 commit comments