Skip to content

Commit 1aaf20f

Browse files
authored
Merge pull request algorithm004-01#394 from uniquenaer/master
181-Week 02
2 parents 4ac9e29 + 9022e2a commit 1aaf20f

8 files changed

+256
-1
lines changed

.DS_Store

6 KB
Binary file not shown.

Week 01/id_181/PriorityQueue.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
## PriorityQueue 优先队列
22

33
> 优先级队列的元素按照其自然顺序进行排序,或者根据构造队列时提供的 Comparator 进行排序
4-
> PriorityQueue 是一个无界队列,但是初始的容量(实际是一个Object[]),随着不断向优先级队列添加元素,其容量会自动扩容,无需指定容量增加策略的细节。
4+
> PriorityQueue 是一个无界队列,但是初始的容量(实际是一个Object[]),随着不断向优先级队列添加元素,其容量会自动扩容,无需指定容量增加策略的细节。
5+
6+
参考文献:
7+
8+
[《Java源码分析》:PriorityQueue - wojiushimogui的博客 - CSDN博客](https://blog.csdn.net/u010412719/article/details/52355557)
9+
[Source for java.util.PriorityQueue (GNU Classpath 0.99.1-pre Documentation)](http://fuseyism.com/classpath/doc/java/util/PriorityQueue-source.html#line.231)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [Python 递归详解 - 从前序与中序遍历序列构造二叉树 - 力扣(LeetCode)](https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# [236. 二叉树的最近公共祖先 - 力扣(LeetCode)](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/)
2+
3+
# Definition for a binary tree node.
4+
# class TreeNode:
5+
# def __init__(self, x):
6+
# self.val = x
7+
# self.left = None
8+
# self.right = None
9+
10+
# 递归
11+
class Solution:
12+
def __init__(self):
13+
self.ans = None
14+
15+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
16+
"""
17+
:type root:TreeNode
18+
:type p:TreeNode
19+
:type q:TreeNode
20+
:rtype:TreeNode
21+
"""
22+
def recurse_tree(curr_node):
23+
if not curr_node:
24+
return False
25+
26+
left = recurse_tree(curr_node.left)
27+
28+
right = recurse_tree(curr_node.right)
29+
30+
mid = curr_node == p or curr_node == q
31+
32+
if(mid + left + right >=2):
33+
self.ans = curr_node
34+
35+
return mid or left or right
36+
37+
recurse_tree(root)
38+
39+
return self.ans
40+
41+
42+
# 父指针迭代
43+
44+
def lowestCommonAncestor(self,root,p,q):
45+
stack = [root]
46+
47+
parent = {root:None}
48+
49+
while p not in parent or q not in parent:
50+
node = stack.pop()
51+
52+
if node.left:
53+
parent[node.left] = node
54+
stack.append(node.left)
55+
56+
if node.right:
57+
parent[node.right] = node
58+
stack.append(node.right)
59+
60+
anccestors = set()
61+
62+
while p:
63+
anccestors.add(p)
64+
p = parent[p]
65+
66+
while q not in anccestors:
67+
q = parent[q]
68+
69+
return q
70+
71+
## 不明觉厉
72+
def lowestCommonAncestor(self, root, p, q):
73+
if root in (None, p, q): return root
74+
left, right = (self.lowestCommonAncestor(kid, p, q)
75+
for kid in (root.left, root.right))
76+
return root if left and right else left or right
77+
78+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// [242. 有效的字母异位词 - 力扣(LeetCode)](https://leetcode-cn.com/problems/valid-anagram/)
2+
3+
/**
4+
借鉴一下使用map实现,
5+
0 s 和 t 的长度判断
6+
1 对s中每个字母进行统计
7+
2 在t时 如果不存在,直接返回false,存在则删除
8+
3 最后判断map的size
9+
10+
ps 非空判断
11+
12+
* @param {string} s
13+
* @param {string} t
14+
* @return {boolean}
15+
*/
16+
var isAnagram = function(s, t) {
17+
if(s.length!==t.length) return false;
18+
if(s.length===0) return true;
19+
const map = new Map();
20+
for(let i =0;i<s.length;i++) {
21+
const getMap = map.get(s[i])+1 || 1;
22+
map.set(s[i],getMap)
23+
}
24+
25+
for(let j = 0;j<t.length;j++) {
26+
const getMap = map.get(t[j]);
27+
if(getMap == undefined ) return false;
28+
console.log(getMap,t[j])
29+
if(getMap===1) {
30+
map.delete(t[j])
31+
}else {
32+
map.set(t[j],getMap-1);
33+
}
34+
35+
}
36+
37+
return !map.size
38+
};
39+
40+
/*
41+
* 排序
42+
**/
43+
44+
var isAnagram = function(s,t){
45+
if(s.length!=t.length) return false;
46+
var str1 = s.split('').sort();
47+
var str2 = t.split('').sort();
48+
return str1.join('') === str2.join('');
49+
}
50+
51+
/**
52+
* 哈希实现
53+
**/
54+
55+
var isAnagram = function(s,t) {
56+
if(s.length!=t.length) return false;
57+
var result = []; // 数组
58+
var aCode = 'a'.charCodeAt();
59+
for(var i = 0;i<s.length;i++) {
60+
var sIndex = s[i].charCodeAt()-aCode,tIndex = t[i].charCodeAt()-aCode;
61+
result[sIndex]=(result[sIndex]||0) + 1;
62+
result[tIndex]=(result[tIndex]||0) - 1;
63+
}
64+
65+
if(result.length===0) return true;
66+
67+
for(var r = 0;r<result.length;r++){
68+
if(!!result[r]){
69+
return false;
70+
}
71+
}
72+
return true;
73+
74+
}
75+
76+
77+
78+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* // Definition for a Node.
3+
* function Node(val,children) {
4+
* this.val = val;
5+
* this.children = children;
6+
* };
7+
*/
8+
/**
9+
* @param {Node} root
10+
* @return {number[]}
11+
*/
12+
13+
// 后序遍历 根 左 右
14+
var postorder = function(root) {
15+
const result = [];
16+
function pushRoot(root) {
17+
if(root.val===null) return result;
18+
if(root.children.length>0) {
19+
for(var i = 0; i<root.children.length;i++) {
20+
pushRoot(root.children[i]);
21+
}
22+
}
23+
result.push(root.val)
24+
}
25+
root && pushRoot(root);
26+
return result;
27+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# [590. N叉树的后序遍历 - 力扣(LeetCode)](https://leetcode-cn.com/problems/n-ary-tree-postorder-traversal/)
2+
class Solution:
3+
def postorder(self, root: 'Node'):
4+
res = []
5+
if root == None: return res
6+
stack = [root]
7+
8+
while stack:
9+
curr = stack.pop()
10+
res.append(curr.val)
11+
stack.extend(curr.children)
12+
print(stack)
13+
14+
15+
return res[::-1]
16+
17+
18+
# 前序排列后的倒序输出
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* [94. 二叉树的中序遍历 - 力扣(LeetCode)](https://leetcode-cn.com/problems/binary-tree-inorder-traversal/submissions/)
3+
* Definition for a binary tree node.
4+
* function TreeNode(val) {
5+
* this.val = val;
6+
* this.left = this.right = null;
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number[]}
12+
中序排序 先拍左子树 再排根节点 最后 右子树
13+
*/
14+
15+
var inorderTraversal = function(root) {
16+
const result = [];
17+
function pushRoot(root){
18+
if(root) {
19+
// 以前写的判断 root.left 为真 才执行加入,发现很耗性能
20+
root.left !== null && pushRoot(root.left)
21+
result.push(root.val);
22+
root.right !== null && pushRoot(root.right)
23+
}
24+
}
25+
pushRoot(root)
26+
27+
return result;
28+
};
29+
30+
// 基于栈的遍历
31+
32+
var inorderTraversal = function(root){
33+
var result = [];
34+
var tmpStack = [];
35+
var currNode = root;
36+
37+
while(currNode!=null || tmpStack.length!=0) {
38+
while(currNode != null) {
39+
tmpStack.push(currNode);
40+
currNode = currNode.left;
41+
}
42+
currNode = tmpStack.pop();
43+
result.push(currNode.val);
44+
currNode = currNode.right;
45+
}
46+
47+
return result;
48+
}

0 commit comments

Comments
 (0)