Golang | Leetcode Golang题解之第402题移掉K位数字

题目:

题解:

Go 复制代码
func removeKdigits(num string, k int) string {
    stack := []byte{}
    for i := range num {
        digit := num[i]
        for k > 0 && len(stack) > 0 && digit < stack[len(stack)-1] {
            stack = stack[:len(stack)-1]
            k--
        }
        stack = append(stack, digit)
    }
    stack = stack[:len(stack)-k]
    ans := strings.TrimLeft(string(stack), "0")
    if ans == "" {
        ans = "0"
    }
    return ans
}
相关推荐
Dream it possible!1 小时前
LeetCode 面试经典 150_栈_简化路径(53_71_C++_中等)(栈+stringstream)
c++·leetcode·面试·
程序员阿鹏2 小时前
49.字母异位词分组
java·开发语言·leetcode
Espresso Macchiato2 小时前
Leetcode 3715. Sum of Perfect Square Ancestors
算法·leetcode·职场和发展·leetcode hard·树的遍历·leetcode 3715·leetcode周赛471
星星点点洲5 小时前
PostgreSQL 15二进制文件
开发语言·设计模式·golang
Miraitowa_cheems9 小时前
LeetCode算法日记 - Day 73: 最小路径和、地下城游戏
数据结构·算法·leetcode·职场和发展·深度优先·动态规划·推荐算法
野蛮人6号9 小时前
力扣热题100道之560和位K的子数组
数据结构·算法·leetcode
Swift社区10 小时前
LeetCode 400 - 第 N 位数字
算法·leetcode·职场和发展
剪一朵云爱着11 小时前
力扣2080. 区间内查询数字的频率
算法·leetcode
youliroam11 小时前
成语接龙学习
学习·golang·uniapp·成语接龙
Dream it possible!11 小时前
LeetCode 面试经典 150_栈_有效的括号(52_20_C++_简单)(栈+哈希表)
c++·leetcode·面试··哈希表