1
1
[#0402-remove-k-digits]
2
- = 402. Remove K Digits
2
+ = 402. 移掉 K 位数字
3
3
4
- { leetcode} /problems/remove-k-digits/[LeetCode - Remove K Digits ^]
4
+ https:// leetcode.cn /problems/remove-k-digits/[LeetCode - 402. 移掉 K 位数字 ^]
5
5
6
- 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.
6
+ 给你一个以字符串表示的非负整数 `num` 和一个整数 `k` ,移除这个数中的 `k` 位数字,使得剩下的数字最小。请你以字符串形式返回这个最小的数字。
7
7
8
- *Note: *
8
+ *示例 1 : *
9
9
10
- * The length of _num_ is less than 10002 and will be ≥ _k_ .
11
- * The given _num_ does not contain any leading zero.
10
+ ....
11
+ 输入:num = "1432219", k = 3
12
+ 输出:"1219"
13
+ 解释:移除掉三个数字 4, 3, 和 2 形成一个新的最小的数字 1219 。
14
+ ....
12
15
13
- *Example 1: *
16
+ *示例 2 : *
14
17
15
- [subs="verbatim,quotes,macros"]
16
- ----
17
- Input: num = "1432219", k = 3
18
- Output: "1219"
19
- Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
20
- ----
18
+ ....
19
+ 输入:num = "10200", k = 1
20
+ 输出:"200"
21
+ 解释:移掉首位的 1 剩下的数字为 200. 注意输出不能有任何前导零。
22
+ ....
21
23
22
- *Example 2: *
24
+ *示例 3 : *
23
25
24
- [subs="verbatim,quotes,macros"]
25
- ----
26
- Input: num = "10200", k = 1
27
- Output: "200"
28
- Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
29
- ----
26
+ ....
27
+ 输入:num = "10", k = 2
28
+ 输出:"0"
29
+ 解释:从原数字移除所有的数字,剩余为空就是 0 。
30
+ ....
30
31
31
- *Example 3:*
32
+ *提示:*
33
+
34
+ * `1 \<= k \<= num.length \<= 10^5^`
35
+ * `num` 仅由若干位数字(0 - 9)组成
36
+ * 除了 *0* 本身之外,`num` 不含任何前导零
32
37
33
- [subs="verbatim,quotes,macros"]
34
- ----
35
- Input: num = "10", k = 2
36
- Output: "0"
37
- Explanation: Remove all the digits from the number and it is left with nothing which is 0.
38
- ----
39
38
40
39
== 思路分析
41
40
42
41
利用单调栈的思路,将前面比当前字符大的字符都删除即可。
43
42
43
+ 利用单调递增栈,将“当前元素”前面更大的元素都删除掉,这样就可以实现尽可能大的减小这个数字。如果数字是递增的,那么就从后面向前删除,直到删够为止。
44
+
44
45
image::images/0402-01.png[{image_attr}]
45
46
46
47
image::images/0402-02.png[{image_attr}]
@@ -71,6 +72,13 @@ image::images/0402-14.png[{image_attr}]
71
72
72
73
image::images/0402-15.png[{image_attr}]
73
74
75
+ 关于单调栈,记住这两句话:
76
+
77
+ * 单调递增栈,利用波谷剔除栈中的波峰,留下波谷;
78
+ * 单调递减栈,利用波峰剔除栈中的波谷,留下波峰。
79
+
80
+ image::images/0402-16.png[{image_attr}]
81
+
74
82
[[src-0402]]
75
83
[tabs]
76
84
====
@@ -83,22 +91,19 @@ include::{sourcedir}/_0402_RemoveKDigits.java[tag=answer]
83
91
----
84
92
--
85
93
86
- // 二刷::
87
- // +
88
- // --
89
- // [{java_src_attr}]
90
- // ----
91
- // include::{sourcedir}/_0402_RemoveKDigits_2.java[tag=answer]
92
- // ----
93
- // --
94
+ 二刷::
95
+ +
96
+ --
97
+ [{java_src_attr}]
98
+ ----
99
+ include::{sourcedir}/_0402_RemoveKDigits_2.java[tag=answer]
100
+ ----
101
+ --
94
102
====
95
103
104
+
96
105
== 参考资料
97
106
98
107
. https://leetcode.cn/problems/remove-k-digits/solutions/484940/yi-diao-kwei-shu-zi-by-leetcode-solution/[402. 移掉 K 位数字 - 官方题解^]
99
108
. https://leetcode.cn/problems/remove-k-digits/solutions/290203/yi-zhao-chi-bian-li-kou-si-dao-ti-ma-ma-zai-ye-b-5/[402. 移掉 K 位数字 - 一招吃遍力扣四道题^]
100
109
. https://leetcode.cn/problems/remove-k-digits/solutions/485036/wei-tu-jie-dan-diao-zhan-dai-ma-jing-jian-402-yi-d/[402. 移掉 K 位数字 - 「手画图解」单调递增栈,为什么?何时用?^]
101
-
102
-
103
-
104
-
0 commit comments