LeetCode OJ 31. Next Permutation
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,23,2,1 → 1,2,31,1,5 → 1,5,1
【题目分析】
题目要求我们对一个数组进行变换,变换后的结果是按照字典序比当前结果大的值中最小的那一个。如果我们不能找到下一个permutation(即当前顺序是这些数字最大的字典序排列),那么直接给出一个逆序的结果。
【思路】
1,2,3 → 1,3,23,2,1 → 1,2,31,1,5 → 1,5,1
分析这几个例子我们发现,如果一段序列是完全降序排列的,那么它的字典序是最大的,即对于这个序列中任意相邻的两个数a和b,a>=b。如果存在这样相邻的两个数a < b,那么通过变换我们可以找到它的下一个permutation。比如1,2,3 -> 1,3,2 我们交换2和3即可得到下一个结果。但是考虑这样一个序列: 4,6,5,我们看到4比6小,如果我们交换3和6得到的结果是6,4,5,这是我们想要的结果吗?并不是,我们发现,虽然相邻的4和6相邻且6比4大,但是在6的右边,还存在一个5它比4大,但是它比6小,如果我们交换5和4,得到的结果才是正确的。
因此这个过程可以这样来描述:
1. 找到第一个相邻的元组(a,b),满足a < b;
2. 如果这样的元组不存在,则将数组逆序,程序结束;
3. 如果这样的元组存在,我们找到从b开始数组以后的数中比a大的最小的那一个c。
4. 交换a和c,然后把c之后所有的数逆序,得到最后的结果,返回;
例如:[4,6,5,3,2,1]
第一步:找到元组(4,6) 4 < 6
第二步:找到6之后比4大的元素中最小的那个:5
第三步:交换4和5->[5,6,4,3,2,1,1]
第四步:把5之后的数字逆序->[5,1,1,2,3,4,6]
通过上述几个步骤,我们能正确地找出任意一个序列的next greater permutation
【java代码】
public class Solution {
public void nextPermutation(int[] nums) {
int len = nums.length;
if(len < 2) return;
//找到第一个降序的数
int index;
for(index = len - 2; index >= 0; index--){
if(nums[index] < nums[index+1]) break;
}
//如果没有这样的数则直接返回逆序的结果
if(index < 0){
for(int i = 0,j = len - 1;i < j; i++, j--){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
return;
}
//找到降序的数的右边比它大的数中最小的那一个
int greater = index + 1;
while(greater < len - 1 && nums[greater + 1] > nums[index]){
greater ++;
}
//第一个降序的数和它右边比他大的最小的那个数交换
int temp = nums[index];
nums[index] = nums[greater];
nums[greater] = temp;
//第一个降序数位置之后所有数逆序,得到最后的结果
for(int i = index + 1, j = len - 1; i < j; i++, j--){
temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
LeetCode OJ 31. Next Permutation的更多相关文章
- <LeetCode OJ> 31. Next Permutation
31. Next Permutation Total Accepted: 54346 Total Submissions: 212155 Difficulty: Medium Implement ne ...
- leetcode个人题解——#31 Next Permutation
写这题时脑子比较混乱,重写了一遍wiki大佬的解法. 算法: According to Wikipedia, a man named Narayana Pandita presented the fo ...
- [Leetcode][Python]31: Next Permutation
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 31: Next Permutationhttps://oj.leetcode ...
- [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 ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
随机推荐
- 【python标准库】内建函数
abs(x) 返回一个数的绝对值.参数可以是普通的整数,长整数或者浮点数.如果参数是个复数,返回它的模. all(iterable) 如果iterable的所有元素为真(或者iterable为空), ...
- CentOS7 安装 OpenSSL 1.0.1m 和 OpenSSH 6.8p1
# 下载软件 wget http://zlib.net/zlib-1.2.8.tar.gz wget ftp://ftp.openssl.org/source/openssl-1.0.1m.tar.g ...
- CodeForces 711D Directed Roads
计数,模拟. 首先观察一下给出的图的特点: $1.$一定存在环. $2.$可能存在多个环. 我们对每个环计算方案数,假设环$C$上包含$x$条边,那么把环$C$破坏掉的方案数有${2^x} - 2$种 ...
- 博客停写,搬家到www.54kaikai.com
博客搬家到自己的网站了www.54kaikai.com欢迎访问.
- 使用vscode对c进行调试
最近在学习C语言.知道vscode支持对c语言的代码的调试,就想试试.然后找了教程: https://code.visualstudio.com/docs/languages/cpp http://w ...
- [Jmeter]jmeter之参数化
一.同一个服务器不同界面访问 a 准备工作: 1.启动jmeter: 2.创建需要访问的url文件,内容示例如下: 即比如:http://www.cnblogs.com/amberly/p/59651 ...
- Chronodex:视觉时间管理,让你的生活更有序
我喜欢把时间安排的有条不紊,看看清晰的时间安排心理有种踏实感,只有你是"纸爱好者" - 才能最终寻找完美组织时间的方式方法. 我记得自从我是一个小女孩以来,我喜欢纸和笔和颜色和标记 ...
- SQL LIKE语句多条件贪婪匹配算法
在CMS开发中,经常会有类似这样的需求: 提问——回答模式,最经典的例子就是百度提问. 提问者提出问题,由其他人回答,其他人可以是用户,也可以是服务商. 在这个模式中,如何充分利用历史数据是最关键的技 ...
- 我喜欢的两个js类实现方式
闭包实现 变量是不会变的:) var myApplication = function(){ var name = 'Yuri'; var age = '34'; var status = 'sing ...
- 为什么重写equals()必须重写hashCode()
主要原因是因为hashCode是用对象的内部地址转换成一个整数的,而equals比较得是两个对象,或者是两个对象的内容 如果你重写了equals,而保留hashCode的实现不变,那么很可能两个对象明 ...