Leetcode全排列问题Java版
2015-08-29 10:16
399 查看
1、编号30 Next Permutation
2、编号44 Permutations
3、编号45 Permutations II
4、编号60 Permutation Sequence
1、编号30 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
public void nextPermutation(Vector<Integer> number){
if(null == number || 1 >= number.size()){
return;
}
else{
int end = number.size()-1;
for(int i = number.size()-2;i>=0;i--){
if(number.get(i) < number.get(end)){
do end--;
while(number.get(i) < number.get(end));
swap(i ,end+1,number);
break;
}
else{
int temp = number.remove(i);
number.add(temp);
}
}
}
for(int i = 0;i<number.size();i++){
System.out.print(number.get(i));
}
}
public void swap(int index,int i,Vector<Integer> v){
int temp = v.get(index);
v.set(index, v.get(i));
v.set(i, temp);
}
2、编号44 Permutations
Given a collection of numbers, return all possible permutations.
For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
public int permutation(int[] collection,int start,int end){
int total = 0;
if(start == end) {
for (int i = 0; i <= end; i++) {
System.out.print(collection[i]);
}
System.out.println();
return 1;
}
for(int i = start;i<=end;i++){
swap(start,i,collection);
total+=permutation(collection,start+1,end);
swap(i,start,collection);
}
return total;
}
3、编号45 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,
[1,1,2] have the following unique permutations:
[1,1,2], [1,2,1], and [2,1,1].
public void permutateDup(int[] collection,int start,int end){
if(start == end ||( (start == end-1) && (collection[start] == collection[end]))) {
for (int i = 0; i <= end; i++) {
System.out.print(collection[i]);
}
System.out.println();
return;
}
for(int i = start;i<=end;i++){
if(collection[i] == collection[start] && i!=start)
continue;
swap(start,i,collection);
permutateDup(collection,start+1,end);
swap(i,start,collection);
}
}
4、编号60 Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note: Given n will be between 1 and 9 inclusive.
public void permutateSequence(int[] set,int k){
int totalCount = permutation(set,1,set.length-1);
int a = (k-1)/totalCount;
int b = (k-1)%totalCount+1;
swap(a,0,set);
int result = permutation(set,0,set.length-1,b,0);
if(-1 != result){
System.out.println("not found");
}
}
public int permutation(int[] collection,int start,int end,int pos,int curPos){
int temp = curPos;
if(start == end ){
curPos++;
if(curPos == pos) {
for (int j = 0; j <= end;j ++) {
System.out.print(collection[j]);
}
System.out.println();
return -1;
}
return 1;
}
else{
for(int i = start;i<=end;i++){
swap(i,start,collection);
int result = permutation(collection,start+1,end,pos,curPos);
if(result == -1){
return -1;
}
else{
curPos+=result;
}
swap(start,i,collection);
}
return curPos-temp;
}
}
2、编号44 Permutations
3、编号45 Permutations II
4、编号60 Permutation Sequence
1、编号30 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
public void nextPermutation(Vector<Integer> number){
if(null == number || 1 >= number.size()){
return;
}
else{
int end = number.size()-1;
for(int i = number.size()-2;i>=0;i--){
if(number.get(i) < number.get(end)){
do end--;
while(number.get(i) < number.get(end));
swap(i ,end+1,number);
break;
}
else{
int temp = number.remove(i);
number.add(temp);
}
}
}
for(int i = 0;i<number.size();i++){
System.out.print(number.get(i));
}
}
public void swap(int index,int i,Vector<Integer> v){
int temp = v.get(index);
v.set(index, v.get(i));
v.set(i, temp);
}
2、编号44 Permutations
Given a collection of numbers, return all possible permutations.
For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
public int permutation(int[] collection,int start,int end){
int total = 0;
if(start == end) {
for (int i = 0; i <= end; i++) {
System.out.print(collection[i]);
}
System.out.println();
return 1;
}
for(int i = start;i<=end;i++){
swap(start,i,collection);
total+=permutation(collection,start+1,end);
swap(i,start,collection);
}
return total;
}
3、编号45 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,
[1,1,2] have the following unique permutations:
[1,1,2], [1,2,1], and [2,1,1].
public void permutateDup(int[] collection,int start,int end){
if(start == end ||( (start == end-1) && (collection[start] == collection[end]))) {
for (int i = 0; i <= end; i++) {
System.out.print(collection[i]);
}
System.out.println();
return;
}
for(int i = start;i<=end;i++){
if(collection[i] == collection[start] && i!=start)
continue;
swap(start,i,collection);
permutateDup(collection,start+1,end);
swap(i,start,collection);
}
}
4、编号60 Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note: Given n will be between 1 and 9 inclusive.
public void permutateSequence(int[] set,int k){
int totalCount = permutation(set,1,set.length-1);
int a = (k-1)/totalCount;
int b = (k-1)%totalCount+1;
swap(a,0,set);
int result = permutation(set,0,set.length-1,b,0);
if(-1 != result){
System.out.println("not found");
}
}
public int permutation(int[] collection,int start,int end,int pos,int curPos){
int temp = curPos;
if(start == end ){
curPos++;
if(curPos == pos) {
for (int j = 0; j <= end;j ++) {
System.out.print(collection[j]);
}
System.out.println();
return -1;
}
return 1;
}
else{
for(int i = start;i<=end;i++){
swap(i,start,collection);
int result = permutation(collection,start+1,end,pos,curPos);
if(result == -1){
return -1;
}
else{
curPos+=result;
}
swap(start,i,collection);
}
return curPos-temp;
}
}
相关文章推荐
- 汇总常见的JAVA错误
- JAVA生成给定范围内的不重复的随机数的几种方法
- 杭电ACM2051java做法
- java综合(五)spring与hibernate整合-事务
- JavaWeb应用中错误和异常处理方法研究
- 【java】基础问题
- 编译、解释与JIT
- date and time
- 省电管理之电源电量显示分析JAVA层
- Java使用SAX,XmlPull两种方式解析XML
- Java 中正确使用 wait, notify 和 notifyAll – 以生产者消费者模型为例
- java多线程 —— 为什么弃用stop和suspend
- [Java Concurrency in Practice]第七章 取消与关闭
- Struts(3)的工作机制及分析
- Struts(2)环境搭建
- SpringMVC详解(一)简介
- Spring MVC详解(二)Spring Web MVC是什么
- Spring MVC详解(三)DispatcherServlet作用
- java 死锁产生原因及解锁
- Spring MVC详解(四) Controller接口控制器详解(1)