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;
}
相关推荐
星释35 分钟前
Rust 练习册 72:多米诺骨牌与回溯算法
开发语言·算法·rust
福尔摩斯张2 小时前
Axios源码深度解析:前端请求库设计精髓
c语言·开发语言·前端·数据结构·游戏·排序算法
算法与编程之美3 小时前
提升minist的准确率并探索分类指标Precision,Recall,F1-Score和Accuracy
人工智能·算法·机器学习·分类·数据挖掘
MicroTech20253 小时前
微算法科技(NASDAQ :MLGO)混合共识算法与机器学习技术:重塑区块链安全新范式
科技·算法·区块链
李牧九丶3 小时前
从零学算法1334
前端·算法
在繁华处3 小时前
C语言经典算法:汉诺塔问题
c语言·算法
le serein —f3 小时前
用go实现-反转链表
leetcode·链表·golang
Bona Sun4 小时前
单片机手搓掌上游戏机(十一)—esp8266运行gameboy模拟器之硬件连接
c语言·c++·单片机·游戏机
酸钠鈀4 小时前
模拟IIC通讯 基于状态机
c语言
爪哇部落算法小助手4 小时前
每日两题day50
数据结构·c++·算法