LeetCode 31 Next Permutation (C,C++,Java,Python)

來源:互聯網
上載者:User

標籤:c   c++   java   python   leetcode   

Problem:

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,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

Solution:採用倒序尋找的方法,尋找到第一個遞減的數字,比如2321,第一個遞減的數字是2,然後在將這個遞減數字後邊的比它大的最小的數字互換,2321就為3221,然後將後邊的數字升序排列,這樣就是了

題目大意:給定一個數組,重新排列這些數字,使得排列好的數字比原來的數字大,並且這個數字是比原來數字大的集合中最小的
Java原始碼(365ms):
public class Solution {    public void nextPermutation(int[] nums) {        int len=nums.length,i=len-1,j,tmp;        while(i>0 && nums[i]<=nums[i-1])i--;        if(i>0){            j=len-1;            while(nums[j]<=nums[i-1])j--;            tmp=nums[j];            nums[j]=nums[i-1];            nums[i-1]=tmp;        }        j=len-1;        while(i<j){            tmp=nums[i];            nums[i]=nums[j];            nums[j]=tmp;            i++;j--;        }    }}

C語言原始碼(14ms):
void nextPermutation(int* nums, int numsSize) {    int j,i=numsSize-1,tmp;    while(i>0 && nums[i]<=nums[i-1])i--;    if(i!=0){        j=numsSize-1;        while(j>=i && nums[j]<=nums[i-1])j--;        tmp=nums[j];        nums[j]=nums[i-1];        nums[i-1]=tmp;    }    j=numsSize-1;    while(i<j){        tmp=nums[i];        nums[i]=nums[j];        nums[j]=tmp;        i++;j--;    }}

C++原始碼(12ms):
class Solution {public:    void nextPermutation(vector<int>& nums) {        int j,len=nums.size(),i=len-1,tmp;        while(i>0 &&  nums[i]<=nums[i-1])i--;        if(i>0){            j=len-1;            while(j>=i && nums[j]<=nums[i-1])j--;            tmp=nums[i-1];            nums[i-1]=nums[j];            nums[j]=tmp;        }        j=len-1;        while(i<j){            tmp=nums[i];            nums[i]=nums[j];            nums[j]=tmp;            i++;j--;        }    }};

Python原始碼(136ms):
class Solution:    # @param {integer[]} nums    # @return {void} Do not return anything, modify nums in-place instead.    def nextPermutation(self, nums):        length=len(nums);i=length-1        while i>0 and nums[i]<=nums[i-1]:i-=1        if i>0:            j=length-1            while nums[j]<=nums[i-1]:j-=1            tmp=nums[j]            nums[j]=nums[i-1]            nums[i-1]=tmp        j=length-1        while i<j:            tmp=nums[i]            nums[i]=nums[j]            nums[j]=tmp            i+=1;j-=1


LeetCode 31 Next Permutation (C,C++,Java,Python)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.