leetcode31. Next Permutation

来源:互联网 发布:gta5娇羞萌妹捏脸数据 编辑:程序博客网 时间:2024/06/05 02:35

题目

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must be in-place, do not allocate extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.

1,2,3 → 1,3,23,2,1 → 1,2,31,1,5 → 1,5,1

思路

从后往前找前面比后面小的数J,位置为index,从最后面找比J大的数,调换位置。再将index后面的数位置调换。
#代码

package leetcodeArray;public class Leetcode31NextPermutation {    public void nextPermutation(int[] nums) {        if(nums.length < 2){            return;        }        int index = nums.length - 1;        while(index > 0 && nums[index-1] >= nums[index] ) index--;        System.out.println(index);        if(index == 0){            reveseOrder(nums, 0, nums.length - 1);        }        else{            int val = nums[index - 1];            int j = nums.length - 1;            while(nums[j] <= val && j>= index)                  j--;            swap(nums,j, index-1);            reveseOrder(nums, index,nums.length - 1);        }    }    void swap(int[] nums, int start, int end){        int temp = nums[start];        nums[start]= nums[end];        nums[end]  = temp;    }    void reveseOrder(int[] nums, int start, int end){        if(start > end)            return;        for(int i = start; i <= (start + end) / 2; i++){            swap(nums, i, start + end - i);        }    }}

结果

这里写图片描述

他山之玉

class Solution {    void nextPermutation(vector<int>& nums) {        int k = -1;        for (int i = nums.size() - 2; i >= 0; i--) {            if (nums[i] < nums[i + 1]) {                k = i;                break;            }        }         if (k == -1) {            reverse(nums.begin(), nums.end());            return;        }        int l = -1;        for (int i = nums.size() - 1; i > k; i--) {            if (nums[i] > nums[k]) {                l = i;                break;            }         }         swap(nums[k], nums[l]);        reverse(nums.begin() + k + 1, nums.end());     }}; 
public class Solution {    public void nextPermutation(int[] nums) {      if(nums.length<=1){          return;      }      int i= nums.length-1;      for(;i>=1;i--){         if(nums[i]>nums[i-1]){ //find first number which is smaller than it's after number             break;         }      }      if(i!=0){          swap(nums,i-1); //if the number exist,which means that the nums not like{5,4,3,2,1}      }      reverse(nums,i);        }    private void swap(int[] a,int i){        for(int j = a.length-1;j>i;j--){            if(a[j]>a[i]){                int t = a[j];                a[j] = a[i];                a[i] = t;                break;            }        }    }    private void reverse(int[] a,int i){//reverse the number after the number we have found        int first = i;        int last = a.length-1;        while(first<last){            int t = a[first];            a[first] = a[last];            a[last] = t;            first ++;            last --;        }    }}
原创粉丝点击