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, ...
随机推荐
- 利用dynamic来提供动态方法的性能
前段时间做了一个worklist的项目,有部分是利用xml配置DICOM的tag,然后根据xml把DICOM的Dataset转为实体类,或者把实体类转为Dataset. 当中主要应用了反射来调用Dat ...
- Poj1159 Palindrome(动态规划DP求最大公共子序列LCS)
一.Description A palindrome is a symmetrical string, that is, a string read identically from left to ...
- linux 下查看某个进程中线程运行在哪个CPU上
运行程序,使用命令top查看指定的进程的PID: 然后使用命令: top -H -p PID 按f键,并使用上下切换,利用空格键选中nTH,P: 按esc键,P所在的列就是线程运行的CPU号:
- UVa11624(逃离火焰问题)
#include"cstdio" #include"queue" #include"cstring" using namespace std ...
- js---复选框(全选,不选,反选)demo1--
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- !important定义为最高级不可替代
<!DOCTYPE html> /*!important定义为最高级不可替代*/ <html lang="en"> <head> <met ...
- eclipse个人觉得有用的快捷键
CTRL+SHIFT+F 自动整理代码格式 CTRL+M 最大/还原当前编辑窗口 CTRL+/ 注释当前行 CTRL+1 快速修复 CTRL+D 删除当前行 SHIFT+ENTER 在当前行前面插入空 ...
- 使用JFileChooser保存文件
--------------------siwuxie095 工程名:TestFileChooser 包名:com.siwuxie095 ...
- Ubuntu 使用 heirloom-mail 调用外部邮箱 SMTP 服务器发送邮件
使用本地服务发邮件,经常被过滤掉而且占用资源,发送成功率不高.所以使用外部SMTP服务器发送邮件成为了需求. SMTP认证的目的是为了使用户避免受到垃圾邮件的侵扰,简单地说就是要求必须在提供了账户名和 ...
- hdu1057
#include <iostream> #include <string> #include <cstring> using namespace std; int ...