https://leetcode.com/contest/weekly-contest-61/problems/monotone-increasing-digits/

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);
    };
};

留言

這個網誌中的熱門文章

https://leetcode.com/contest/leetcode-weekly-contest-52/problems/longest-univalue-path/

https://leetcode.com/contest/leetcode-weekly-contest-52/problems/maximum-sum-of-3-non-overlapping-subarrays/