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
}
相关推荐
Kuo-Teng3 分钟前
LeetCode 19: Remove Nth Node From End of List
java·数据结构·算法·leetcode·链表·职场和发展·list
Kuo-Teng7 分钟前
LeetCode 21: Merge Two Sorted Lists
java·算法·leetcode·链表·职场和发展
元亓亓亓2 小时前
LeetCode热题100--39. 组合总和
算法·leetcode·职场和发展
2401_841495642 小时前
【LeetCode刷题】找到字符串中所有字母异位词
数据结构·python·算法·leetcode·数组·滑动窗口·找到字符串中所有字母异位词
橘颂TA2 小时前
【剑斩OFFER】算法的暴力美学——寻找数组的中心下标
算法·leetcode·职场和发展·结构与算法
py有趣2 小时前
LeetCode算法学习之鸡蛋掉落
学习·算法·leetcode
Kuo-Teng4 小时前
LeetCode 141. Linked List Cycle
java·算法·leetcode·链表·职场和发展
资深web全栈开发4 小时前
力扣2536子矩阵元素加1-差分数组解法详解
算法·leetcode·矩阵·golang·差分数组
·云扬·5 小时前
【LeetCode Hot 100】 136. 只出现一次的数字
算法·leetcode·职场和发展
熬了夜的程序员7 小时前
【LeetCode】114. 二叉树展开为链表
leetcode·链表·深度优先