forked from zhuli19901106/lintcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete-digits(AC).cpp
40 lines (39 loc) · 938 Bytes
/
delete-digits(AC).cpp
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
class Solution {
public:
/**
*@param A: A positive integer which has N digits, A is a string.
*@param k: Remove k digits.
*@return: A string
*/
string DeleteDigits(string A, int k) {
string ans = "";
int n = A.length();
int c = n - k;
int i, j;
i = 0;
while (i < n) {
j = i + 1;
while (j < n && k > 0) {
if (A[j] >= A[i]) {
++j;
continue;
}
if (k >= j - i) {
k -= j - i;
i = j;
}
++j;
}
ans.push_back(A[i++]);
if (ans.length() >= c) {
break;
}
}
i = 0;
while (i < c - 1 && ans[i] == '0') {
++i;
}
ans = ans.substr(i, c - i);
return ans;
}
};