赞
踩
- class Solution {
- public:
- int monotoneIncreasingDigits(int n) {
- string s = to_string(n);
- int flag = s.size();
- for (int i = s.size() - 1; i > 0; i--) {
- if (s[i] < s[i - 1]) {
- flag = i;
- s[i - 1]--;
- }
- }
- for (int i = flag; i < s.size(); i++) {
- s[i] = '9';
- }
- return stoi(s);
- }
- };

考虑好遍历顺序。
学习stoi()、to_string()函数的运用。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。