[LeetCode] 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,2
3,2,1
→ 1,2,3
1,1,5
→ 1,5,1
解法:
这道题要求下一个排列顺序,也就是全排列中的下一个(如:123的全排列为123、132、213、231、312、321,给定其中一个,输出其下一个),如果给定数组是降序,则说明是全排列的最后一种情况,则下一个排列就是最初始情况,全排列知识可以参见:全排列生成算法:next_permutation。我们再来看下面一个例子,有如下的一个数组
1 2 7 4 3 1
下一个排列为:
1 3 1 2 4 7
那么是如何得到的呢,我们通过观察原数组可以发现,如果从末尾往前看,数字逐渐变大,到了2时才减小的,然后我们再从后往前找第一个比2大的数字,是3,那么我们交换2和3,再把此时3后面的所有数字转置一下即可,步骤如下:
1 7 4 3 1
1 7 4 1
1 7 4 1
1 3 1 2 4 7
public class Solution {
public void nextPermutation(int[] nums) {
if (nums == null || nums.length == 0) {
return;
}
int index = nums.length - 2;
for (; index >= 0; index--) {
if (nums[index] < nums[index + 1]) {
break;
}
}
if (index == -1) {
Arrays.sort(nums);
return;
} int biggerIndex = nums.length - 1;
for (; biggerIndex >= 0; biggerIndex--) {
if (nums[biggerIndex] > nums[index]) {
break;
}
} swap(nums, index, biggerIndex);
reverse(nums, index + 1, nums.length - 1);
} public void swap(int[] num, int i, int j) {
int temp = num[i];
num[i] = num[j];
num[j] = temp;
} public void reverse(int[] num, int begin, int end) {
for (int i = begin, j = end; i < j; i++, j--) {
swap(num, i, j);
}
}
}
[LeetCode] 31. Next Permutation ☆☆☆的更多相关文章
- [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] 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 permuta ...
- 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 per ...
- [leetcode]31. Next Permutation下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
随机推荐
- 2017年度网络安全服务企业TOP50
何谓“大安全”? 近几年来,网络安全和信息安全领域不时出现引发社会各界关注的事件. 2014年,政府采购计划对WIN8说“不”,同年,中央网络安全和信息化领导小组成立,将网络安全上升到了国家战略高度, ...
- 【探路者】Alpha发布用户使用报告
预期统计用户使用数量:13人. 博文内容:1用户列表.2评论列表.3统计与总结 1用户列表: 二.评论内容 用户1:1不够好看.2不应该是中国地图为背景,蛇头是人物头像的么?(那是宣传片,不是预览图) ...
- mininet实验 脚本实现控制交换机行为
写在前面 本文参考 通过这个实验,我学习到了另一种下流表的方式. 下流表有两种方式(我目前了解): 通过controller下发. 通过OvS提供的API直接向OvS交换机下流表. 本实验脚本已经把相 ...
- 【线段树维护复杂状态】Ryuji doesn't want to study
https://nanti.jisuanke.com/t/31460 tree[rt].ans = tree[rt << 1].ans + tree[rt << 1 | 1]. ...
- HDU 1121 Complete the Sequence 差分
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1121 Complete the Sequence Time Limit: 3000/1000 MS ...
- PHP中与类有关的几个魔术常量
与类有关的魔术常量: 以前学过的魔术常量: __FILE__ __DIR__ __LINE__ 现在: __CLASS__: 代表当前其所在的类的类名: __METHOD__:代表其当前所在的方法名:
- Web服务器负载均衡的几种方案 : DNS轮询
本篇主要讲一下最简单的方案——DNS轮询. DNS轮询 大多域名注册商都支持多条A记录 的解析,其实这就是DNS轮询 ,DNS 服务器 将解析请求按照A记录 的顺序,逐一分配到不同的IP上,这样就完成 ...
- brush
简介 Brushing是一个通过点击或触摸来选择一个一维或二维区域的交互操作,比如可以通过点击鼠标并移动. brush经常被用来选择离散的元素比如散点图中的点或桌面上的文件等.它也可以被用来放大选中的 ...
- 【bzoj3203】[Sdoi2013]保护出题人 凸包+二分
题目描述 输入 第一行两个空格隔开的正整数n和d,分别表示关数和相邻僵尸间的距离.接下来n行每行两个空格隔开的正整数,第i + 1行为Ai和 Xi,分别表示相比上一关在僵尸队列排头增加血量为Ai 点的 ...
- 【BZOJ5333】荣誉称号(动态规划)
[BZOJ5333]荣誉称号(动态规划) 题面 BZOJ 洛谷 题解 今天早上贱狗老师讲的.然而我还是不会. 只好照着\(zsy\)代码大力理解一波. 首先观察等式,如果比较熟悉线段树,会发现就是线段 ...