Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Apr 11, 2024
1 parent b6939f8 commit 6f1c291
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions leetcode/402.RemoveKDigits/sol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// https://leetcode.com/problems/remove-k-digits

package main

import (
"fmt"
)

func removeKdigits(num string, k int) string {
stk, remain := make([]byte, 0), len(num)-k
for i := 0; i < len(num); i++ {
n := len(stk)
for k > 0 && n > 0 && stk[n-1] > num[i] {
stk = stk[:n-1]
n, k = n-1, k-1
}
stk = append(stk, num[i])
}

for i := 0; i < len(stk) && i < remain; i++ {
if stk[i] != '0' {
return string(stk[i:remain])
}
}
return "0"
}

func main() {
num := "1432219"
k := 3

fmt.Println(removeKdigits(num, k))
}

0 comments on commit 6f1c291

Please sign in to comment.