LeetCode //C - 402. Remove K Digits

402. Remove K Digits

Given string num representing a non-negative integer num, and an integer k, return the smallest possible integer after removing k digits from num.

Example 1:

Input: num = "1432219", k = 3
Output: "1219"
Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.

Example 2:

Input: num = "10200", k = 1
Output: "200"
Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.

Example 3:

Input: num = "10", k = 2
Output: "0"
Explanation: Remove all the digits from the number and it is left with nothing which is 0.

Constraints:
  • 1 < = k < = n u m . l e n g t h < = 1 0 5 1 <= k <= num.length <= 10^5 1<=k<=num.length<=105
  • num consists of only digits.
  • num does not have any leading zeros except for the zero itself.

From: LeetCode

Link: 402. Remove K Digits


Solution:

Ideas:

- Stack Approach: The key idea is to use a stack to build the smallest number. If a digit is greater than the next digit, we remove it from the stack to get a smaller result.
- Efficiency: The algorithm runs in linear time, O(n), where n is the length of the input number, making it efficient for large inputs.
- Edge Cases: Properly handles cases where the entire number is removed, or there are leading zeros.

Code:
c 复制代码
char* removeKdigits(char* num, int k) {
    int len = strlen(num);
    
    // If k equals or exceeds the number of digits, the result is 0
    if (k >= len) {
        char* result = (char*)malloc(2);
        result[0] = '0';
        result[1] = '\0';
        return result;
    }

    // Allocate memory for the stack with a length of len + 1 (for null-terminator)
    char* stack = (char*)malloc((len + 1) * sizeof(char));
    int top = 0;  // Index to keep track of the top of the stack

    for (int i = 0; i < len; i++) {
        // While there are characters in the stack, and the current digit is smaller than the stack's top
        // and we still need to remove digits (k > 0), we pop the stack.
        while (top > 0 && k > 0 && stack[top - 1] > num[i]) {
            top--;
            k--;
        }
        // Add the current digit to the stack if it's not a leading zero or the stack is not empty
        if (top > 0 || num[i] != '0') {
            stack[top++] = num[i];
        }
    }

    // If we still need to remove more digits, reduce the size of the stack
    top -= k;

    // If there are no digits left, return "0"
    if (top <= 0) {
        free(stack);
        char* result = (char*)malloc(2);
        result[0] = '0';
        result[1] = '\0';
        return result;
    }

    // Null-terminate the stack and return it as the result
    stack[top] = '\0';

    return stack;
}
相关推荐
快手技术几秒前
打破信息茧房!快手搜索多视角正样本增强引擎 CroPS 入选 AAAI 2026 Oral
后端·算法·架构
e***98571 分钟前
MATLAB高效算法实战:从基础到进阶优化
开发语言·算法·matlab
CoderCodingNo6 分钟前
【GESP】C++五级练习(前缀和练习) luogu-P1387 最大正方形
开发语言·c++·算法
MicroTech202514 分钟前
MLGO微算法科技通过 Lindbladians 设计线性微分方程的近似最优量子算法——开放量子系统框架下的量子ODE求解新范式
科技·算法·量子计算
知乎的哥廷根数学学派20 分钟前
基于多尺度特征提取和注意力自适应动态路由胶囊网络的工业轴承故障诊断算法(Pytorch)
开发语言·网络·人工智能·pytorch·python·算法·机器学习
源代码•宸23 分钟前
Leetcode—85. 最大矩形【困难】
经验分享·算法·leetcode·职场和发展·golang·单调栈
平哥努力学习ing35 分钟前
《数据结构》-第八章 排序
数据结构·算法·排序算法
CoovallyAIHub35 分钟前
为AI装上“纠偏”思维链,开源框架Robust-R1显著提升多模态大模型抗退化能力
深度学习·算法·计算机视觉
小棠师姐44 分钟前
随机森林原理与实战:如何解决过拟合问题?
算法·机器学习·随机森林算法·python实战·过拟合解决
你怎么知道我是队长1 小时前
C语言---预处理器
c语言·开发语言·chrome