算法训练day39LeetCode738.968.
738.单调递增的数字
题目
题解
-
class Solution { public: int monotoneIncreasingDigits(int n) { string strNum = to_string(n); // int 转换string int flag = strNum.size(); for (int i = strNum.size() - 1; i > 0; i--) { if (strNum[i - 1] > strNum[i]) { flag = i; strNum[i - 1]--; } } for (int i = flag; i < strNum.size(); i++) { strNum[i] = '9'; } return stoi(strNum); } };