My 12ms c++ solution only 15lines


  • 6
    S
    class Solution {
    public:
        void nextPermutation(vector<int>& nums) {
            int index = 0,swapindex = 0;
            for (int i = 1; i < nums.size(); i++)
                if  (nums[i - 1] < nums[i])
                    index = i;
            if (index > 0){
                int i = nums.size() - 1;
                while (i > (index - 1) && nums[index-1] >= nums[i]) i--;
                swap(nums[index - 1],nums[i]);
            }
            sort(nums.begin() + index,nums.end());
        }
    };

  • -14
    S

    only 6 lines

      class Solution {
        public:
            void nextPermutation(vector<int>& nums) {
                next_permutation(nums.begin(),nums.end());
            }
        };

  • 0
    M

    。。。。。。。。。。。。。。。。。。。。。。。。。。


Log in to reply
 

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