Algorithms—46.Permutations
2015-06-11 15:54
337 查看
思路:排列组合
public class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> answerList=new ArrayList<List<Integer>>();
for (int i = 0; i < nums.length; i++) {
int size=answerList.size();
if (size==0) {
for (int j = 0; j < nums.length; j++) {
List<Integer> intList=new ArrayList<Integer>();
intList.add(nums[j]);
answerList.add(intList);
}
}else {
for (int j = 0; j < size; j++) {
List<Integer> intList=answerList.get(j);
Map<Integer,String> map=new HashMap<Integer,String>();
for (int l = 0; l <intList.size(); l++) {
map.put(intList.get(l), "存在");
}
for (int k = 0; k < nums.length; k++) {
if (map.get(nums[k])==null) {
System.out.println("k="+k);
List<Integer> newList=new ArrayList<Integer>();
for (int l = 0; l < intList.size(); l++) {
newList.add(intList.get(l));
}
newList.add(nums[k]);
System.out.println();
answerList.add(newList);
}
}
}
if (size!=answerList.size()) {
for (int j = size-1; j >=0; j--) {
answerList.remove(j);
}
}
}
}
return answerList;
}
}
public class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> answerList=new ArrayList<List<Integer>>();
for (int i = 0; i < nums.length; i++) {
int size=answerList.size();
if (size==0) {
for (int j = 0; j < nums.length; j++) {
List<Integer> intList=new ArrayList<Integer>();
intList.add(nums[j]);
answerList.add(intList);
}
}else {
for (int j = 0; j < size; j++) {
List<Integer> intList=answerList.get(j);
Map<Integer,String> map=new HashMap<Integer,String>();
for (int l = 0; l <intList.size(); l++) {
map.put(intList.get(l), "存在");
}
for (int k = 0; k < nums.length; k++) {
if (map.get(nums[k])==null) {
System.out.println("k="+k);
List<Integer> newList=new ArrayList<Integer>();
for (int l = 0; l < intList.size(); l++) {
newList.add(intList.get(l));
}
newList.add(nums[k]);
System.out.println();
answerList.add(newList);
}
}
}
if (size!=answerList.size()) {
for (int j = size-1; j >=0; j--) {
answerList.remove(j);
}
}
}
}
return answerList;
}
}
相关文章推荐
- Algorithms—55.Jump Game
- 悲剧!Homebrew 的作者被 Google 拒啦,因为他不会翻转二叉树(转自V2EX)
- 使用UliPad进行Django项目开发
- 为什么科技巨头们纷纷更换 Logo?
- Algorithm --> Kruskal算法和Prim算法
- [MapReduce] Google三驾马车:GFS、MapReduce和Bigtable
- 更换Prezi的logo
- go语言常用函数
- google建立多人同时编辑的文档或者表格
- 【Leetcode Algorithm】Reverse Linked List
- 【Leetcode Algorithm】Roman to Integer
- Hangover
- Hangover 分类: POJ 2015-06-11 10:34 12人阅读 评论(0) 收藏
- google 搜索结果在新标签页打开
- mongoengine
- POJ 1042 Gone Fishing (贪心)(刘汝佳黑书)
- 终极 Web 应用性能和压力测试工具 Gor
- MIGO+103收货到GR冻结库存和MB1B+344从非限制到冻结区别?
- Algorithms
- VIEWGOOD(远古)军队演习实训远程高清直播的应用