Share my ten line code


  • 0
    void nextPermutation(vector<int> &num) {
            int ie = num.size() - 1;
            while(ie > 0 && num[ie-1] >= num[ie]) ie--;
            if(ie == 0){
                sort(num.begin(), num.end()); return;
            }
            int temp = num[ie-1];int ir = ie+1;
            while(ir<num.size() && num[ir]>temp)ir++;
            num[ie-1] = num[ir-1];num[ir-1] = temp;
            sort(num.begin()+ie,num.end());
        }

Log in to reply
 

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