Next Permutation

Total Accepted: 33595 Total Submissions: 134095

 
 

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.

解题思路:

题目不难,关键是理解题目的意思: 如果把nums看做一个数字的话,即返回比原来数大的最小的数(如果没有(原数是降序排列)则返回升序排列)。

也就是字典排序,JAVA实现如下:

	static public void nextPermutation(int[] nums) {
int index = nums.length - 1;
while (index >= 1) {
if (nums[index] > nums[index - 1]) {
int swapNum=nums[index-1],swapIndex = index+1;
while (swapIndex <= nums.length - 1&& swapNum < nums[swapIndex])
swapIndex++;
nums[index-1]=nums[swapIndex-1];
nums[swapIndex-1]=swapNum;
reverse(nums,index);
return;
}
index--;
}
reverse(nums,0);
}
static void reverse(int[] nums,int swapIndex){
int[] swap=new int[nums.length-swapIndex];
for(int i=0;i<swap.length;i++)
swap[i]=nums[nums.length-1-i];
for(int i=0;i<swap.length;i++)
nums[swapIndex+i]=swap[i];
}

Java for LeetCode 031 Next Permutation的更多相关文章

  1. Java for LeetCode 060 Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  2. Java for LeetCode 047 Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  3. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  4. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  5. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  6. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  7. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  8. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  9. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

随机推荐

  1. BZOJ solve 100 纪念

    按照xiaoyimi立下的flag是不是该去表白啦--可惜并没有妹子

  2. 【长期更新】--神犇的BLOGS(各种高端讲解)

    KMP字符串匹配算法: http://kb.cnblogs.com/page/176818/ http://blog.csdn.net/yutianzuijin/article/details/119 ...

  3. 解决SSH无密码登陆后又需要密码登陆

    主节点CentOS_Master 从节点Slave_1. 我想着可能是 /etc/ssh/sshd_config下的那个公钥文件路径不对,看了下home/hxsyl/.ssh/authorized_k ...

  4. Android系统中的广播(Broadcast)机制简要介绍和学习计划

    在Android系统中,广播(Broadcast)是在组件之间传播数据(Intent)的一种机制:这些组件甚至是可以位于不同的进程中,这样它就像Binder机制一样,起到进程间通信的作用:本文通过一个 ...

  5. groovy-位运算

    从Groovy 1.0 beta 10开始,Groovy支持位运算:<<. >>, >>>, |, &, ^, and ~. 下表列出了位运算的操作符 ...

  6. python运行报错:urllib2.URLError: <urlopen error [Errno 10061] >

    Traceback (most recent call last): File "F:\adt-bundle-windows-x86_64-20140702\eclipse\workspac ...

  7. 自动打包iOS项目

    基于Lexrus的博文iOS-makefile,本文对自动打包涉及到的操作步骤以及理论基础进行了适当的补充.     请在阅读本文前先阅读<iOS makefile>.文章地址:http: ...

  8. 初试visual studio2012的新型数据库LocalDB

    http://www.cnblogs.com/zhangran/archive/2012/08/21/2649200.html 今天在vs2012里面打开以前的mvc3项目,结果弹出警告说在vs201 ...

  9. MyEclipse------各种问题解决方法

    1.汉化后如何变为英文版:找到myeclipse.ini文件,改为:language=enlanguage=zh为中文 2.解决版本不匹配问题:http://blog.sina.com.cn/s/bl ...

  10. spark-submit提示资源不足

    ensure that workers are registered and have sufficient resources spark-cluster启动的配置里配置了每个worker的内存,如 ...