Very easy to understand code(c++)


  • 0
    C
    class Solution {
    public:
        int maximumSwap(int num) {
            string s = to_string(num);
            string reference = s;
            sort(reference.rbegin(),reference.rend());
            for(int i = 0; i<s.length();i++){
                if(s[i]!=reference[i]){
                    size_t index = s.rfind(reference[i]);
                    swap(s[i],s[index]);
                    break;
                }
            }
            return stoi(s);
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.