Next Permutation 



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

思路:此题是我眼下做过的leetCode中感觉最难的题,它的难在于算法非常难自己短时间实现,假设曾经没有做过这种题,差点儿非常难非常快的有思路解出来。

在參考网上资料之前,我也尝试了好几种解法。可是没有一种能达到效果。最后參考资料。才恍然:这尼玛也能够这样做,可是臣妾非常难想到啊!

题目的总体思路是。从后往前读。当后面的数比前面的数大时,在开一个循环,从后開始于当前数比較。当比当前数大时,交换。然后再从当前数的后一位開始,直到最后反序就可以。

详细代码和凝视例如以下:

public class Solution {
public void nextPermutation(int[] nums) {
if(nums.length <= 1){
return;
}
for(int i = nums.length - 1;i > 0; i--){
if(nums[i] > nums[i-1]){//假设nums[i] > nums[i-1]
//再从后往前推断有否数字比nums[i-1]大
int j = nums.length - 1;
for(; j >= i -1;j--){
if(nums[j] > nums[i-1]){
break;//如有,则结束循环。保留j的值
}
}
//将nums[j]和nums[i-1]交换
int temp = nums[j];
nums[j] = nums[i-1];
nums[i-1] = temp; //从i開始反序(即交换位置的地方開始反序)
for(j = 0; j < (nums.length - (i))/2;j++){
int k = nums.length - 1 - j;
int m = nums[j+i];
nums[j+i] = nums[k];
nums[k] = m;
}
return;
}
}
//假设到最后没有交换的数据,则说明是降序排列,须要升序排列
for(int i = 0; i < nums.length/2;i++){
int j = nums.length - 1 - i;//双指针排序,交换首尾相应的两两数据就可以
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}

leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法的更多相关文章

  1. [LeetCode] 31. Next Permutation 下一个排列

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

  2. [leetcode]31. Next Permutation下一个排列

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

  3. 2.1.12 Next Permutation 下一个字典序数组

    对当前排列从后向前扫描,找到一对为升序的相邻元素,记为i和j(i < j).如果不存在这样一对为升序的相邻元素,则所有排列均已找到,算法结束:否则,重新对当前排列从后向前扫描,找到第一个大于i的 ...

  4. leetCode 90.Subsets II(子集II) 解题思路和方法

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  5. 31. Next Permutation (下一个全排列)

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

  6. leetCode 75.Sort Colors (颜色排序) 解题思路和方法

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  7. leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法

    Swap Nodes in Pairs  Given a linked list, swap every two adjacent nodes and return its head. For exa ...

  8. leetCode 87.Scramble String (拼凑字符串) 解题思路和方法

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  9. leetCode 85.Maximal Rectangle (最大矩阵) 解题思路和方法

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

随机推荐

  1. how to remove MouseListener / ActionListener on a JTextField

    I have the following code adding an ActionListener to a JTextField: chatInput.addMouseListener(new j ...

  2. DM368启动串口打印分析

    DM36x initialization passed! TI UBL Version: 1.50 Booting Catalog Boot Loader                  //启动目 ...

  3. #pragma pack(n) 的作用

    在C语言中,结构是一种复合数据类型,其构成元素既可以是基本数据类型(如int.long.float等)的变量,也可以是一些复合数据类型(如数组.结构.联合等)的数据单元.在结构中,编译器为结构的每个成 ...

  4. 中国省市位置描述JSON数据

    数据包括:省.市.区县的行政编码,以及经纬度位置. {}对象的属性描述,如:{"no":"450400","latlng":"23 ...

  5. Python中and和or

    转自:http://unei66.blog.163.com/blog/static/544640292010320745886/ python中的and和or 4.6. and 和 or 的特殊性质在 ...

  6. Unity doesn't load, no Launcher, no Dash appears

    1. 重新安装 ubuntu-desktop不起作用. Enter the following commands:- Ctrl+Alt+F1 login there by user name and ...

  7. 【C/C++】Linux下使用system()函数一定要谨慎

    [C/C++]Linux下使用system()函数一定要谨慎 http://my.oschina.net/renhc/blog/53580 曾经的曾经,被system()函数折磨过,之所以这样,是因为 ...

  8. python 魔法方法之:__getitem__ __setitem__ __delitem__

    h2 { color: #fff; background-color: #7CCD7C; padding: 3px; margin: 10px 0px } h3 { color: #fff; back ...

  9. C#基础:委托 【转】

    委托是C#中最为常见的内容.与类.枚举.结构.接口一样,委托也是一种类型.类是对象的抽象,而委托则可以看成是函数的抽象.一个委托代表了具有相同参数列表和返回值的所有函数.比如: delegate in ...

  10. Stopwatch 和TimeSpan介绍【转】

    1.使用 Stopwatch 类 (System.Diagnostics.Stopwatch) Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间.在典型的 S ...