Paste your text here.class Solution {
public:
int monotoneIncreasingDigits(int N) {
std::string s = std::to_string(N);
std::string result;
int max = 0;
int i, j;
int last = -1;
for (i = 0; i < s.length(); ++i)
{
if (s.at(i) > max){
max = s.at(i);
last = i;
} else if (s.at(i) == max) {
continue;
} else {
break;
}
}
if (i != s.length()) {
for (j = 0; j < last; j++) {
result += s.at(j);
}
if ((s.at(j) - 1) != '0')
result += (s.at(j) - 1);
for (int j = last + 1; j < s.length(); j++) {
result += '9';
}
} else {
result = s;
}
return std::stoi(result, nullptr, 10);
};
};
留言
張貼留言