题目
解答
#include <iostream>
#include <string>
#include <list>
#include <cassert>
std
::string
removeKdigits(const std
::string
& num
, int k
) {
std
::list
<char> min_number
;
if (num
.empty()) {
return "0";
}
for (char digit
: num
) {
while (!min_number
.empty() && k
> 0) {
if (min_number
.back() > digit
) {
min_number
.pop_back();
--k
;
} else {
break;
}
}
if (min_number
.empty() && digit
== '0') {
continue;
}
min_number
.push_back(digit
);
}
while (k
> 0 && !min_number
.empty()) {
min_number
.pop_back();
--k
;
}
std
::string min_number_str
;
for (char min_digit
: min_number
) {
min_number_str
.push_back(min_digit
);
}
if (min_number
.empty()) {
return "0";
}
return min_number_str
;
}
int main() {
std
::cout
<< removeKdigits("54321", 1) << std
::endl
;
std
::cout
<< removeKdigits("12345", 1) << std
::endl
;
std
::cout
<< removeKdigits("16543", 1) << std
::endl
;
std
::cout
<< removeKdigits("1", 1) << std
::endl
;
std
::cout
<< removeKdigits("54321", 2) << std
::endl
;
std
::cout
<< removeKdigits("12345", 2) << std
::endl
;
std
::cout
<< removeKdigits("16543", 2) << std
::endl
;
std
::cout
<< removeKdigits("165436", 3) << std
::endl
;
std
::cout
<< removeKdigits("10001", 1) << std
::endl
;
std
::cout
<< removeKdigits("10001", 3) << std
::endl
;
std
::cout
<< removeKdigits("10000", 3) << std
::endl
;
return 0;
}
转载请注明原文地址:https://tech.qufami.com/read-19582.html