Next Permutation leetcode java
题目:
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
题解:
本文讲解转自Code Ganker稍稍修改“http://blog.csdn.net/linhuanmars/article/details/20434115”
“这道题是给定一个数组和一个排列,求下一个排列。算法上其实没有什么特别的地方,主要的问题是经常不是一见到这个题就能马上理清思路。下面我们用一个例子来说明,比如排列是(2,3,6,5,4,1),求下一个排列的基本步骤是这样:
1) 先从后往前找到第一个不是依次增长的数,记录下位置p。比如例子中的3,对应的位置是1;
2) 接下来分两种情况:
(1) 如果上面的数字都是依次增长的,那么说明这是最后一个排列,下一个就是第一个,其实把所有数字反转过来即可(比如(6,5,4,3,2,1)下一个是(1,2,3,4,5,6));
(2) 否则,如果p存在,从p开始往后找,找找找,找到第一个比他小的数,然后两个调换位置,比如例子中的4。调换位置后得到(2,4,6,5,3,1)。最后把p之后的所有数字倒序,比如例子中得到(2,4,1,3,5,6),
这个即是要求的下一个排列。
i--;
j++;
j--;
swap(num,i,j);
}
reverse(num, i+1,num.length-1);
}
num[i] = num[j];
num[j] = tmp;
}
swap(num, i++, j--);
}
Next Permutation leetcode java的更多相关文章
- Next Permutation - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Next Permutation - LeetCode 注意点 如果是字典序最大的串则要返回字典序最小的串 解法 解法一:参见:http://www.cn ...
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- LeetCode算法题-Letter Case Permutation(Java实现)
这是悦乐书的第315次更新,第336篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第184题(顺位题号是784).给定一个字符串S,将每个字母单独转换为小写或大写以创建另 ...
- Permutation Sequence leetcode java
题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- Regular Expression Matching leetcode java
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- Sqrt(int x) leetcode java
Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735 题目: Implement int sqrt(int x). Co ...
- 60. Permutation Sequence (JAVA)
The set [1,2,3,...,n] contains a total of n! unique permutations. By listing and labeling all of the ...
- ZigZag Conversion leetcode java
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
随机推荐
- python之比较is与==(转载)
在 Python 中会用到对象之间比较,可以用 ==,也可以用 is .但是它们的区别是什么呢? is 比较的是两个实例对象是不是完全相同,它们是不是同一个对象,占用的内存地址是否相同.莱布尼茨说过: ...
- java 动态代理(类型信息)
代理是基本的设计模式之一它为你提供额外的或不同的操作,而插入的用来代替"实际"对象的对象. package typeinfo; //: typeinfo/SimpleProxyDe ...
- hdu2289二分答案 圆台体积
精度小一点就能过 #include<bits/stdc++.h> #define maxn 1000000009 #define esp 1e-9 #define PI 3.1415926 ...
- 有关列分组,定义css样式无效的问题
声明: web前端学习笔记,欢迎大神指点.联系QQ:1522025433. 有时候我们要单独对表格的某列定义单独的样式,就会很自然的想到 表格的 在<table>标签内使用的 <co ...
- JavaScript中的短路
短路:逻辑运算从左到右.逻辑或运算,当左边的条件成立时,后面的条件将不再参与运算. 因此在逻辑或运算中,尽量将条件结果为true的放第一位.而在逻辑与运算中,尽量将条件结果为false的放到第一位. ...
- canvas扩散圆环
最近看了很多牛的动画,想想自己的canvas的确很菜. 公式在那里,但是不是太会套.找demo发现都是很难的 于是找了个简单的效果 圆环从中间扩散的效果 关键是 globalCompositeOper ...
- ZOJ-3537
题目大意:给你一个n (n<=300) 边形,给出它所有的顶点坐标,让你把它划分成n-2个三角形的花费最小值,顶点 a 和 b 相连的花费为 abs(a.x+b.x)*abs(a.y+b.y). ...
- C语言 提取double的每一位
#include<stdio.h> int main() { double x = 256.141592654; ; //整数部分 while(n) //整数部分输出 { ; n /= ; ...
- 《Gradle权威指南》--Gradle构建脚本基础
No1: 设置文件默认名是setting.gradle,放在根目录下,大多数作用都是为了配置子工程 No2: 一个Project包含很多个Task.Task就是一个操作,一个原子性的操作.其实它是Pr ...
- IPv6升级改造包括什么?
关于IPv6技术升级具体做了什么,在我脑海里只是更换了域名解析的IP而已,所以找了这篇文章,帮助初步解惑. 原文地址:https://www.maczd.com/post/web-ipv6-upgra ...