题目描述:
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].
思路解析:
https://blog.youkuaiyun.com/Lynn_Baby/article/details/79736771
跟这个题是比较类似的
- 首先需要有一个临时的item来一一添加num[i],然后长度达到num.length时,就可以添加进入res了
- 然后每个num[i]需要标记是否访问过,所以需要用到boolean类型的数组
- 注意添加后,需要回退之前的item
代码:
import java.util.*;
public class Solution {
public ArrayList<ArrayList<Integer>> permute(int[] num) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> item = new ArrayList<Integer>();
if(num==null || num.length==0){
return res;
}
boolean[] visited = new boolean[num.length];
helper(res,item,num,visited);
return res;
}
private void helper(ArrayList<ArrayList<Integer>> res , ArrayList<Integer> item,int[] num,boolean[] visited){
if(item.size() == num.length){
res.add(new ArrayList<Integer>(item));
return;
}else{
for(int i=0;i<num.length;i++){
if(visited[i]==false){
item.add(num[i]);
visited[i]=true;
helper(res,item,num,visited);
item.remove(item.size()-1);
visited[i]=false;
}
}
}
}
}
Combinations
题目描述:
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
思路解析:
- 给出一个n和k,要求求出由n中k个不同的数组成是序列,序列为升序排序。
- 这道题要求所有可能的情况,显然我们可以使用回溯的方法求解,对于每次判断的边界条件为:后面的数要大于前面的数。而且由于这里1到n肯定是递增的,所以继续进行下一层运算的条件可以为 当前位置后面的数。
代码:
import java.util.*;
public class Solution {
public ArrayList<ArrayList<Integer>> combine(int n, int k) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if(n<=0||n<k)
return res;
ArrayList<Integer> list = new ArrayList<Integer>();
dfs(res,list,1,n,k);
return res;
}
public void dfs(ArrayList<ArrayList<Integer>> res,ArrayList<Integer> list,int start,int n,int k){
if(list.size()== k){
res.add(new ArrayList<Integer>(list));
return;
}
for(int i=start;i<=n;i++){
list.add(i);
dfs(res,list,i+1,n,k);
list.remove(list.size()-1);
}
}
}
permutations-sequence(全排列的第k个)
题目描述:
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 k thpermutation sequence.
Note: Given n will be between 1 and 9 inclusive.
思路解析:
- 先把1到n的每个数存入list中,然后后边写一个数字,就可以从里面删掉一个了。
- n个数字有n!个排列方式,n-1就有(n-1)!个排列方式,
- 为了和下标保持一致,需要k--,k/(n-1)!是当前字符的下标(注意是从零开始啊,下标0对应的数字1)
- 下一次的k就可以更新为 k%(n-1)!,循环n次
- 需要一个times表示每次更新阶乘的除数。
代码:
import java.util.*;
public class Solution {
public String getPermutation(int n, int k) {
k--;
List<Integer> list = new ArrayList<Integer>();//注意存储1-n
StringBuilder s = new StringBuilder();
int times = n-1;
for(int i=1;i<=n;i++){
list.add(i);
}
int factorail = 1;//阶乘
for(int i=2;i<n;i++){//不要×n
factorail*=i;
}
while(times>=0){
int indexList = k/factorail;
s.append(list.get(indexList));
list.remove(indexList);
k=k%factorail;
if(times!=0){
factorail/=times;
}
times--;
}
return s.toString();
}
}