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;
}
相关推荐
ゞ 正在缓冲99%…13 分钟前
leetcode76.最小覆盖子串
java·算法·leetcode·字符串·双指针·滑动窗口
xuanjiong14 分钟前
纯个人整理,蓝桥杯使用的算法模板day2(0-1背包问题),手打个人理解注释,超全面,且均已验证成功(附带详细手写“模拟流程图”,全网首个
算法·蓝桥杯·动态规划
小郝 小郝25 分钟前
【C语言】strstr查找字符串函数
c语言·开发语言
惊鸿.Jh33 分钟前
【滑动窗口】3254. 长度为 K 的子数组的能量值 I
数据结构·算法·leetcode
明灯L33 分钟前
《函数基础与内存机制深度剖析:从 return 语句到各类经典编程题详解》
经验分享·python·算法·链表·经典例题
碳基学AI39 分钟前
哈尔滨工业大学DeepSeek公开课:探索大模型原理、技术与应用从GPT到DeepSeek|附视频与讲义免费下载方法
大数据·人工智能·python·gpt·算法·语言模型·集成学习
补三补四43 分钟前
机器学习-聚类分析算法
人工智能·深度学习·算法·机器学习
独好紫罗兰1 小时前
洛谷题单3-P5718 【深基4.例2】找最小值-python-流程图重构
开发语言·python·算法
正脉科工 CAE仿真1 小时前
基于ANSYS 概率设计和APDL编程的结构可靠性设计分析
人工智能·python·算法
Dovis(誓平步青云)2 小时前
【数据结构】排序算法(中篇)·处理大数据的精妙
c语言·数据结构·算法·排序算法·学习方法