031 Next Permutation 下一个排列
实现获取下一个排列函数,这个算法需要将数字重新排列成字典序中数字更大的排列。
如果不存在更大的排列,则重新将数字排列成最小的排列(即升序排列)。
修改必须是原地的,不开辟额外的内存空间。
这是一些例子,输入位于左侧列,其相应输出位于右侧列。
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1
详见:https://leetcode.com/problems/next-permutation/description/
Java实现:
class Solution {
public void nextPermutation(int[] nums) {
if(nums==null || nums.length==0){
return;
}
int i = nums.length-2;
while(i>=0 && nums[i]>=nums[i+1]) {
--i;
}
if(i>=0){
int j=i+1;
while(j<nums.length && nums[j]>nums[i]){
++j;
}
--j;
swap(nums,i,j);
}
reverse(nums, i+1,nums.length-1);
}
private void swap(int[] nums,int i,int j){
int tmp=nums[i];
nums[i]=nums[j];
nums[j]=tmp;
}
private void reverse(int[] nums,int i,int j){
while(i<j){
swap(nums,i++,j--);
}
}
}
参考:http://www.cnblogs.com/grandyang/p/4428207.html
031 Next Permutation 下一个排列的更多相关文章
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【LeetCode每天一题】Next Permutation(下一个排列)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- [LeetCode] 31. Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- lintcode:next permutation下一个排列
题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] ...
- [leetcode]31. Next Permutation下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- [Swift]LeetCode31. 下一个排列 | Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 力扣——Next Permutation(下一个排列) python实现
题目描述: 中文: 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许 ...
- Leetcode31--->Next Permutation(数字的下一个排列)
题目: 给定一个整数,存放在数组中,求出该整数的下一个排列(字典顺序):要求原地置换,且不能分配额外的内存 举例: 1,2,3 → 1,3,2: 3,2,1 → 1,2,3: 1,1,5 → 1, ...
随机推荐
- HBase的bulkLoad
HBase的BulkLoad有两种方式: thinrow的机制是flatmap把cell的信息进行flatmap:适合少于1万列的数据集:thinrow的涵义就是少行多列: bulkload的机制则是 ...
- YARN指令
如果是使用了Cloudera来安装到此路径下: /opt/cloudera/parcels/CDH-5.10.2-1.cdh5.10.2.p0.5/bin 执行: sudo ./yarn applic ...
- BZOJ1455:罗马游戏
题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=1455 浅谈左偏树:https://www.cnblogs.com/AKMer/p/102466 ...
- POJ3468(线段树区间维护)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 85502 ...
- ios判断是否为iphone6或iphone6plus代码
转自:http://blog.csdn.net/lvxiangan/article/details/45288505 #define IS_IPAD (UI_USER_INTERFACE_IDIOM( ...
- BadImageFormatException,未能加载正确的程序集XXX的解决办法
BadImageFormatException,未能加载正确的程序集XXX的解决办法 IDE:VS2010 语言:C# 异常:System.BadImageFormatException,未能加载正确 ...
- JSP相关背景
-----------------siwuxie095 Sun Microsystems SUN 即 Stanford ...
- KickStart安装CentOS,同时安装和配置hadoop
声明:这篇文章是前面是拾人牙慧,我是结合 http://www.111cn.net/sys/linux/59969.htm 和 http://www.cnblogs.com/mchina/p/cent ...
- PCLVisualizer可视化类(2)
博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=163 可视化点云颜色特征 所示,点赋予不同的颜色表征其对应的z轴值不同.PC ...
- 【转】WebElement.getText()为空解决方法
WebElement.getText()为空解决方法 当使用getText()获取一个普通的链接文本时: <a href="http://www.baidu.com"> ...