码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-05-14 16:38:48      阅读:146      评论:0      收藏:0      [点我收藏+]

标签: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)

标签:c   c++   java   python   leetcode   

原文地址:http://blog.csdn.net/runningtortoises/article/details/45720473

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!