Skip to content

Commit 7060d50

Browse files
authored
Merge pull request algorithm004-01#428 from jie96169/master
611-Week 02
2 parents 9c0d8e9 + f4ff4f7 commit 7060d50

File tree

6 files changed

+211
-0
lines changed

6 files changed

+211
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
12+
HashMap<Integer,Integer> map = new HashMap();
13+
int[] preorder ;
14+
15+
//使用hashset保存前序遍历,加快查询效率
16+
public TreeNode buildTree(int[] preorder, int[] inorder) {
17+
int preLen = preorder.length;
18+
int inLen = inorder.length;
19+
if(preLen != inLen){
20+
return null;
21+
}
22+
this.preorder = preorder;
23+
for(int i = 0; i < inorder.length; i++){
24+
map.put(inorder[i],i);
25+
}
26+
return buildTree(0,preLen-1,0,inLen);
27+
}
28+
29+
public TreeNode buildTree( int preLeft, int preRight, int inLeft, int inRight){
30+
//终止条件
31+
if(preLeft > preRight || inLeft > inRight){
32+
return null;
33+
}
34+
35+
//根据前序遍历,找到跟节点位置,并生成跟节点
36+
int rootVal = preorder[preLeft];
37+
TreeNode rootNode = new TreeNode(rootVal);
38+
//找到下一层级的跟节点位置
39+
int nextRootIdx = map.get(rootVal);
40+
41+
rootNode.left = buildTree(preLeft + 1, nextRootIdx + preLeft -inLeft, inLeft, nextRootIdx -1);
42+
rootNode.right = buildTree(nextRootIdx + preLeft -inLeft +1, preRight, nextRootIdx + 1,inRight );
43+
44+
return rootNode;
45+
}
46+
47+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
3+
//初始化数据
4+
HashMap<String,String> map = new HashMap(){{
5+
put("2", "abc");
6+
put("3", "def");
7+
put("4", "ghi");
8+
put("5", "jkl");
9+
put("6", "mno");
10+
put("7", "pqrs");
11+
put("8", "tuv");
12+
put("9", "wxyz");
13+
}};
14+
15+
16+
public List<String> letterCombinations(String digits) {
17+
if(digits.length() != 0){
18+
digits("",digits);
19+
}
20+
return output;
21+
}
22+
23+
//先用回溯实现
24+
public void digits(String combition,String next_digits){
25+
if(next_digits.length() == 0){
26+
output.add(combition);
27+
return;
28+
} else {
29+
String num = next_digits.substring(0,1);
30+
String letters = map.get(num);
31+
for(int i = 0; i < letters.length(); i++){
32+
String letter = letters.substring(i,i+1);
33+
digits(combition + letter, next_digits.substring(1));
34+
}
35+
36+
}
37+
}
38+
39+
List<String> output = new ArrayList();
40+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
//传说中的递归
12+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
13+
//终止条件
14+
//向上回溯条件
15+
if(root == null || root.val ==p.val || root.val == q.val) return root;
16+
17+
TreeNode left = lowestCommonAncestor(root.left,p,q);
18+
TreeNode right = lowestCommonAncestor(root.right,p,q);
19+
20+
//判断是否到底
21+
if(left == null) return right;
22+
if(right == null) return left;
23+
//
24+
return root;
25+
26+
}
27+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
//回溯+剪枝+状态重置
3+
public List<List<Integer>> permute(int[] nums) {
4+
int len = nums.length;
5+
List<List<Integer>> resList = new ArrayList();
6+
if(len == 0){
7+
return resList;
8+
}
9+
10+
//状态记忆
11+
boolean[] used = new boolean[nums.length+1];
12+
//路径记忆
13+
Stack<Integer> path = new Stack();
14+
15+
geneatePermute( nums, resList, 0, len, used, path);
16+
return resList;
17+
}
18+
19+
20+
public void geneatePermute(int[] nums,List<List<Integer>> resList,int curSize,int len,boolean[] used,Stack<Integer> path){
21+
if(curSize == len){
22+
resList.add(new ArrayList(path));
23+
return ;
24+
}
25+
26+
for(int i = 0; i < len; i++){
27+
//根据状态,判断分支是否已经记忆
28+
if(!used[i]){
29+
used[i] = true;
30+
path.add(nums[i]);
31+
//curSize 为每层独立空间中的数据
32+
geneatePermute(nums,resList,curSize + 1,len,used,path);
33+
//状态重置
34+
used[i] = false;
35+
path.pop();
36+
}
37+
}
38+
}
39+
40+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
3+
boolean[] used ;
4+
List<List<Integer>> resList = new ArrayList();
5+
public List<List<Integer>> permuteUnique(int[] nums) {
6+
int n = nums.length;
7+
if(n == 0) return resList;
8+
Arrays.sort(nums);
9+
10+
used = new boolean[n];
11+
findpermuteUnique(nums,0,new Stack<Integer>());
12+
return resList;
13+
}
14+
15+
public void findpermuteUnique(int[] nums,int dept, Stack<Integer> path){
16+
if(dept == nums.length){
17+
resList.add(new ArrayList(path));
18+
return;
19+
}
20+
21+
for(int i = 0; i < nums.length; i++){
22+
if(!used[i]){
23+
//复合剪枝操作
24+
if(i > 0 && nums[i] == nums[i-1] && !used[i-1]) continue;
25+
used[i] = true;
26+
path.add(nums[i]);
27+
findpermuteUnique(nums,dept + 1,path);
28+
used[i] = false;
29+
path.pop();
30+
}
31+
}
32+
}
33+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
List<List<Integer>> res = new LinkedList();
3+
int n;
4+
int k;
5+
//最普通的回溯解法,肝不动了,先撸出来再说。。。
6+
public List<List<Integer>> combine(int n, int k) {
7+
this.n = n;
8+
this.k = k;
9+
backtrack(new LinkedList(),1);
10+
return res;
11+
}
12+
13+
public void backtrack(LinkedList<Integer> list,int first){
14+
if(list.size() == k){
15+
res.add(new LinkedList(list));
16+
}
17+
18+
for(int i = first; i <= n; ++i){
19+
list.add(i);
20+
backtrack(list,i+1);
21+
list.removeLast();
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)