[LeetCode] 402. Remove K Digits

Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.

Note:

  • The length of num is less than 10002 and will be ≥ k.
  • The given num does not contain any leading zero.

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.

移掉K位数字。

题意是给一个以字符串表示的数字和一个数字K,请你从字符串中移除K位数字,使得剩下的部分组成的数字是最小的。

思路是贪心,会用到单调栈。什么样的数字才是最小的?首先高位的数字需要尽可能的低,所以想到用stack,从左往右把每一位push到stack,当stack不为空且栈顶元素比要push进去的元素要大的时候,pop出栈顶元素,直到丢弃了K个数字为止。但是这个题也有一些corner case,比如第二个例子,10200,放弃一个数字,按照之前的思路,会丢弃掉1,但是剩下的部分是0200,需要处理掉开头的0;再看第三个例子,10,需要去掉2个数字,会使得res成为0。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public String removeKdigits(String num, int k) {
 3         // corner case
 4         if (k == num.length()) {
 5             return "0";
 6         }
 7 
 8         // normal case
 9         Stack<Character> stack = new Stack<>();
10         for (int i = 0; i < num.length(); i++) {
11             while (k > 0 && !stack.isEmpty() && stack.peek() > num.charAt(i)) {
12                 stack.pop();
13                 k--;
14             }
15             stack.push(num.charAt(i));
16         }
17 
18         while (k > 0) {
19             stack.pop();
20             k--;
21         }
22 
23         StringBuilder sb = new StringBuilder();
24         while (!stack.isEmpty()) {
25             sb.append(stack.pop());
26         }
27         sb.reverse();
28 
29         int res = 0;
30         // skip the leading zeros
31         while (res < sb.length() && sb.charAt(res) == '0') {
32             res++;
33         }
34         return res == sb.length() ? "0" : sb.substring(res);
35     }
36 }

JavaScript实现

 1 /**
 2  * @param {string} num
 3  * @param {number} k
 4  * @return {string}
 5  */
 6 var removeKdigits = function(num, k) {
 7     // corner case
 8     if (num === null || num.length === 0) {
 9         return '0';
10     }
11 
12     // normal case
13     let stack = [];
14     for (let i = 0; i < num.length; i++) {
15         while (k > 0 && stack.length && num.charAt(i) < stack[stack.length - 1]) {
16             stack.pop();
17             k--;
18         }
19         stack.push(num.charAt(i));
20     }
21 
22     while (k > 0) {
23         stack.pop();
24         k--;
25     }
26 
27     let sb = stack.join('');
28     let res = 0;
29     while (res < sb.length && sb.charAt(res) === '0') {
30         res++;
31     }
32     return res === sb.length ? '0' : sb.substring(res);
33 };

LeetCode 题目总结

原文地址:https://www.cnblogs.com/cnoodle/p/12886100.html