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. spark快速入门之最简配置 spark 1.5.2 hadoop 2.7 配置

    配置的伪分布式,ubuntu14.04上 先配置hadoop,参见这个博客,讲的很好 http://www.powerxing.com/install-hadoop/, 但是我在配的过程中还是遇到了问 ...

  2. read write spinlock

    发一个自己基于 C++11 写的 read write spinlock,在 MinGW 4.8.2 (gcc 4.8 全面支持c++ 11,但由于gcc windows平台 libstdc++ 目前 ...

  3. [转]ArcGIS移动客户端离线地图的几种解决方案

    原文地址:http://blog.chinaunix.net/uid-10914615-id-3023158.html 移动GIS中,通常将数据分为两大类:basemap layer和operatio ...

  4. 关于k-means聚类算法的matlab实现

    在数据挖掘中聚类和分类的原理被广泛的应用. 聚类即无监督的学习. 分类即有监督的学习. 通俗一点的讲就是:聚类之前是未知样本的分类.而是根据样本本身的相似性进行划分为相似的类簇.而分类 是已知样本分类 ...

  5. Linux内核中常见内存分配函数(三)

    ioremap void * ioremap (unsigned long offset, unsigned long size) ioremap是一种更直接的内存“分配”方式,使用时直接指定物理起始 ...

  6. IE8对css文件的限制

    很多人在写css时,时常把很多css样式放到一个文件中.也有些框架在上线后,能对很多css文件进行合并.这样能减少对服务器的请求次数,从而加快服务器的响应速度.在IE8中,当css的规则个数大于409 ...

  7. 移动web开发前准备知识了解(html5、jquery)笔记

    1.经常使用 插件工具  chrome插件:   Mobile & Tablet Emulator(用于常见移动端适配):(重点) Mobile Emulator is an useful o ...

  8. 用cssText属性批量操作样式

    给一个HTML元素设置css属性,如 var head= document.getElementById("head"); head.style.width = "200 ...

  9. 系统分层 manager层意义

    manager用于控制事务,通常是这么说的,但是如果把事务空指针service可以的,但是有些时候,service加了Transaction注解之后,在加别的注解,可能导致Transaction失效. ...

  10. 使用Devexpress中的CharControl控件,需要控制AxisY轴的显示范围,需要使用该控件的BoundDataChanged事件

    一.控制ChartControl的Y轴范围 使用Devexpress中的CharControl控件,需要控制AxisY轴的显示范围,需要使用该控件的BoundDataChanged事件,具体代码如下: ...