leetcode 31. Next Permutation JAVA
题目:
实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。
如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。
必须原地修改,只允许使用额外常数空间。
以下是一些例子,输入位于左侧列,其相应输出位于右侧列。1,2,3
→ 1,3,2
3,2,1
→ 1,2,3
1,1,5
→ 1,5,1
【0】【1】【2】
解题思路:
题目的总体思路是。从后往前读。当后面的数比前面的数大时,再开一个循环,从后开始于当前数比較。当比当前数大时,交换。然后再从当前数的后一位開始,直到最后反序就可以。
- class Solution {
- public void nextPermutation(int[] nums) { //高位为nums[0]
- if(nums != null && nums.length >1){
- int i;
- for(i = nums.length-2;i>=0;i--){
- if(nums[i+1]>nums[i]){
- break;
- }
- }
- if(i >= 0){ //如果整个序列为逆序时,i小于0 reverse整个序列,否则找到比nums[i]大的交换次序
- int k;
- for(k=nums.length-1;k>=0;k--){
- if(nums[k]>nums[i]){
- break;
- }
- }
- swap(nums,k,i);
- }
- reverse(nums, i+1);
- }
- }
- private void reverse(int[] nums, int start) {
- int left = start;
- int right = nums.length - 1;
- while (left < right) {
- swap(nums, left, right);
- left++;
- right--;
- }
- }
- private void swap(int[] nums, int i, int j) {
- int temp = nums[i];
- nums[i] = nums[j];
- nums[j] = temp;
- }
- }
leetcode 31. Next Permutation JAVA的更多相关文章
- [array] leetcode - 31. Next Permutation - Medium
leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- LeetCode - 31. Next Permutation
31. Next Permutation Problem's Link ---------------------------------------------------------------- ...
- leetcode 31. Next Permutation (下一个排列,模拟,二分查找)
题目链接 31. Next Permutation 题意 给定一段排列,输出其升序相邻的下一段排列.比如[1,3,2]的下一段排列为[2,1,3]. 注意排列呈环形,即[3,2,1]的下一段排列为[1 ...
- Java [leetcode 31]Next Permutation
题目描述: Implement next permutation, which rearranges numbers into the lexicographically next greater p ...
- LeetCode 31. 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 ...
- leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically ne ...
- leetcode 31. Next Permutation(字典序的下一个)
描述: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...
随机推荐
- iOS学习之UIPickerView控件的关联选择
接上篇iOS学习之UIPickerView控件的简单使用 接着上篇的代码 http://download.csdn.net/detail/totogo2010/4391870 ,我们要实现的效果如下: ...
- rocketmq消息存储概述
了解消息存储部分首先需要关注的几个方法,load()--Load previously stored messages.start()--Launch this message store.putMe ...
- spring4-2-bean配置-4-bean之间的关系
- 关于select Count()的使用和性能问题
比如Count(*) FROM E_Table WHERE [date] > '2008-1-1' AND istrue = 0 由于操作的数据比较大(400万以上),所以使用了两个数据库,一个 ...
- CF 662C Binary Table
用FWT优化计算. 首先发现行数很小,想到一个暴力的方法,就是以一个二进制位$0$表示这一行不翻转而二进制位$1$表示这一行翻转,然后$2^n$枚举出所有行的翻转情况,再$O(m)$计算所有的结果. ...
- 最小生成树 prim
1.算法思想: 图采用邻接矩阵存储,贪心找到目前情况下能连上的权值最小的边的另一端点,加入之,直到所有的顶点加入完毕. 2.算法实现步骤: 设图G =(V,E),其生成树的顶点集合为U. (1)把v0 ...
- NPOI读写Excel sheet操作
QueryInfo dataInfo = new QueryInfo(); dataInfo.CustomSQL = $@" select t1.name name,t1.url url f ...
- easyui tabs update 强制刷新页面
var tab = artDialog.open.origin.$("#tabs").tabs("getTab", "公司评级"); var ...
- CentOS7.4配置SSH登录密码与密钥身份验证踩坑
简单记录,自用CentOS7.4虚拟机与ALiYunVPS,在配置ssh登录身份验证时碰到的问题. 阿里云VPS:因为在重置磁盘时选择了密钥对的身份验证方式,因此VPS中的CentOS7.4中的 /e ...
- springdata -----操作ES
一:配置springdata-Es elasticseach-JPA.xml <?xml version="1.0" encoding="UTF-8"?& ...