leetcode — next-permutation
import java.util.Arrays;
/**
* Source : https://oj.leetcode.com/problems/next-permutation/
*
* Created by lverpeng on 2017/7/13.
*
* * 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
*
*/
public class NextPermutation {
/**
* 寻找多个数字组成的数字序列中的下一个,比如:1,2,3组成的序列
* 123,132,213,231,312,312
*
* 规律如下:
* 从右向左找到第一个num[i] > num[i-1]
* 然后从右向左找到第一个num[k] > num[i-1]
* 交换两个位置
* 对num[i-1]后面元素排序
*
* 边界条件:i = 1的时候还没找到num[i-1]就把整个数字的各个位翻转顺序
*
*
* @param num
* @return
*/
public void nextPermutation (int[] num) {
for (int i = num.length - 1; i > 0; i--) {
if (num[i] > num[i-1]) {
int k = num.length - 1;
while (num[i-1] > num[k]) {
k --;
}
// swap
int temp = num[i-1];
num[i-1] = num[k];
num[k] = temp;
reverse(num, i, num.length - 1);
return ;
}
// 边界情况,已经是最大了,转化为最小
if (i == 1) {
reverse(num, 0, num.length - 1);
return ;
}
}
}
private void reverse (int[] arr, int start, int end) {
if (start < 0 || end > arr.length - 1 || start > end) {
return;
}
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
end --;
start ++;
}
}
public static void main(String[] args) {
NextPermutation nextPermutation = new NextPermutation();
int[] arr1 = new int[]{1, 2, 3, 4};
nextPermutation.nextPermutation(arr1);
System.out.println(Arrays.toString(arr1));
int[] arr2 = new int[]{1, 3, 2, 4};
nextPermutation.nextPermutation(arr2);
System.out.println(Arrays.toString(arr2));
int[] arr3 = new int[]{4,3,2,1};
nextPermutation.nextPermutation(arr3);
System.out.println(Arrays.toString(arr3));
}
}
leetcode — next-permutation的更多相关文章
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode Palindrome Permutation II
原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/ 题目: Given a string s, return all th ...
- LeetCode Palindrome Permutation
原题链接在这里:https://leetcode.com/problems/palindrome-permutation/ 题目: Given a string, determine if a per ...
- 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 ...
- [LeetCode] Find Permutation 找全排列
By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decre ...
- [leetcode]Next Permutation @ Python
原题地址:https://oj.leetcode.com/problems/next-permutation/ 题意: Implement next permutation, which rearra ...
- LeetCode Find Permutation
原题链接在这里:https://leetcode.com/problems/find-permutation/description/ 题目: By now, you are given a secr ...
随机推荐
- svn的基本使用方法
一,svn的介绍 Subversion(SVN) 是一个开源的版本控制系統, 也就是说 Subversion 管理着随时间改变的数据. 这些数据放置在一个中央资料档案库(repository) 中. ...
- linux从0开始----01
1.VMware 虚拟机安装与卸载 推荐安装较高版本,11.x以后的.本课程安装12.x版本,需要序列号. 2.在vmware中安装centos客户机.初学者选择典型安装也可以. 1.vware文件菜 ...
- eclipse启动时要求高版本jdk的问题
在eclipse.ini文件首行添加 -vm C:\Program Files\Java\jdk1.8\jdk1.8.0_131\bin https://blog.csdn.net/wanlin77/ ...
- outlook VSTO
https://files.cnblogs.com/files/maooveyu/OutlookPhotoSync.rar
- 《python语言程序设计》_第二章编程题
2.1 程序: Celsius=eval(input("Enter a degree in Celsius:"))#输入摄氏度的值Celsiusfahrenheit =(9/5)* ...
- Delegate、Thread、Task、ThreadPool几种方式创建异步任务性能对比
开始预测的结果是 Task>Delegate>ThreadPool>>Thread. (一)测试代码 static async Task<int> AsyncTas ...
- Python之旅Day15 Bootstrap与Django初识
Bootstrap初识 Bootstrap简介 Bootstrap 是最受欢迎的 HTML.CSS 和 JS 框架,用于开发响应式布局.移动设备优先的 WEB 项目.(官网http://www.boo ...
- 背水一战 Windows 10 (97) - 选取器: CachedFileUpdater
[源码下载] 背水一战 Windows 10 (97) - 选取器: CachedFileUpdater 作者:webabcd 介绍背水一战 Windows 10 之 选取器 CachedFileUp ...
- Docker应用:Docker-compose(容器编排)
阅读目录: Docker应用:Hello World Docker应用:Docker-compose(容器编排) 前言: 昨天完成了Docker入门示例(Docker应用:Hello World),示 ...
- 关于Spring Cloud的核心特性
SOA和微服务的区别 其实服务化架构已经可以解决大部分企业的需求了,那么我们为什么要研究微服务呢?先说说它们的区别: 微服务架构强调业务系统需要彻底的组件化和服务化,一个组件就是一个产品,可以独立对外 ...