-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveK_Digit.cpp
More file actions
51 lines (42 loc) · 1.18 KB
/
RemoveK_Digit.cpp
File metadata and controls
51 lines (42 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
402. Remove K Digits leetcode
``````````````````````````````
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.
*/
#include<iostream>
using namespace std;
string removeKdigits(string num, int k) {
string ans="";
for(char &c:num)
{
while(ans.size() && ans.back()>c &&k)
{
ans.pop_back();
k--;
}
if(ans.size()||c!='0')
ans.push_back(c);
}
while(ans.size()&&k--)
{
ans.pop_back();
}
return (ans=="")?"0":ans;
}
int main()
{
string num;
cin>>num;
int k;
cin>>k;
cout<<removeKdigits(num,k);
}