LeetCode 46. Permutations
来源:互联网 发布:微信java通用版1.0精简 编辑:程序博客网 时间:2024/06/05 20:22
LeetCode 46. Permutations
Given a collection of distinct 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],
[3,2,1]
]
思路一:通过遍历线性表每次选择一个元素将其加入链表,删除该元素并递归执行,当线性表中没有元素是结束递归。
public class Node{ intval; Nodenext; publicNode(int val){ this.val= val; } }public voiddfs(List<List<Integer>> res, Node head, List<Integer> ans){ if(head.next==null) {res.add(newArrayList<Integer>(ans)); return;} Node p = head, q=head.next; while(q!=null){ ans.add(q.val); p.next = q.next; dfs(res,head,ans); ans.remove(ans.size()-1); p.next = q; p = p.next; q= q.next; }}
完整代码:
class Solution { publicclass Node{ intval; Nodenext; publicNode(int val){ this.val= val; } } public List<List<Integer>> permute(int[] nums) { List<List<Integer>> res = newArrayList<List<Integer>>(); if(nums.length == 0) return res; Arrays.sort(nums); Node head= new Node(0); Node p=head; for(int i=0;i<nums.length;i++){ Node tmp=newNode(nums[i]); p.next = tmp; p = p.next; } dfs(res,head,new ArrayList<Integer>()); return res; } public voiddfs(List<List<Integer>> res, Node head, List<Integer> ans){ if(head.next==null) {res.add(newArrayList<Integer>(ans)); return;} Node p = head, q=head.next; while(q!=null){ ans.add(q.val); p.next = q.next; dfs(res,head,ans); ans.remove(ans.size()-1); p.next = q; p = p.next; q= q.next; } }}
思路二:通过每次交换当前元素i 与 i+k元素递归调用:
private void backtrack(int[]nums,List<List<Integer>> result,List<Integer> res,int index) { if(index==nums.length) { result.add(newArrayList<Integer>(res)); return; } for(int i=index;i<nums.length;i++) { swap(nums,index,i); res.add(nums[index]); backtrack(nums,result,res,index+1); res.remove(res.size()-1); swap(nums,index,i); }}
完整代码:
class Solution { public List<List<Integer>> permute(int[] nums) { List<List<Integer>> result = new ArrayList<>(); List<Integer> res = new ArrayList<>(); if(nums!=null && nums.length>0) backtrack(nums,result,res,0); return result; } private void backtrack(int[] nums,List<List<Integer>>result,List<Integer> res,int index) { if(index==nums.length) { result.add(new ArrayList<Integer>(res)); return; } for(int i=index;i<nums.length;i++) { swap(nums,index,i); res.add(nums[index]); backtrack(nums,result,res,index+1); res.remove(res.size()-1); swap(nums,index,i); } } private void swap(int[] nums,int i,int j) { int tmp = nums[i]; nums[i]=nums[j]; nums[j]=tmp; }}
阅读全文
0 0
- [LeetCode]46.Permutations
- LeetCode 46.Permutations
- 46. Permutations Leetcode Python
- LeetCode --- 46. Permutations
- [Leetcode] 46. Permutations
- [leetcode] 46.Permutations
- LeetCode 46. Permutations DFS
- 46. Permutations LeetCode
- Leetcode 46. Permutations
- leetcode 46. Permutations
- LeetCode *** 46. Permutations
- LeetCode 46. Permutations
- leetcode 46. Permutations
- LeetCode - 46. Permutations
- 【leetcode】46. Permutations
- leetcode 46. Permutations
- [LeetCode] 46. Permutations
- leetcode 46. Permutations
- 【MongoDB+docker】docker 配置 mongodb 复制集
- dwz框架常用记录
- 基于SnapDragonBoard410c的远程防盗系统 (二)
- MySQL 主从复制详解(详细)
- 关于性能优化问题的探究
- LeetCode 46. Permutations
- [JAVA]单元测试:从零开始的JUnit之路(Elicpse/IDEA)
- 深度学习框架Keras学习系列(一):线性代数基础与numpy使用(Linear Algebra Basis and Numpy)
- 三级缓存
- 自定义video
- 莫烦PyTorch学习笔记(一)——Torch或Numpy
- 面试题总结(1)
- 《集体智慧编程》数学公式
- less常用