Skip to content

Commit 5c52912

Browse files
committed
add leetcode: offer-64-68.1-68.2
1 parent d0994e3 commit 5c52912

5 files changed

+191
-4
lines changed

Algorithm/src/acwing/basic/chapter2/_3302ExpressionEvaluation.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@
3636
*/
3737
public class _3302ExpressionEvaluation {
3838
/*流程:
39-
1.从左至右遍历表达式,数字和(直接压入对应的栈,只不过数字需要另外的判别是不是多位数,如
39+
1.从左至右遍历表达式,数字和(直接压入对应的num栈,只不过数字需要另外的判别是不是多位数,如
4040
983,这个时候就需要把这个983从字符串中处理出来。
4141
2.如果是)说明已经有一个()里面的表达式子部分可以处理出结果,一直进行运算,直到对应的(
42-
出现,并把它弹出栈,运算结果存入数字栈
43-
3.如果是运算符,只有在运算符栈不空并且栈顶不是(,栈顶优先级大于当前运算符优先级,才进行
42+
出现,并把它弹出栈,运算结果存入数字栈num
43+
3.如果是运算符,只有在运算符栈op不空并且栈顶不是(,栈顶优先级大于当前运算符优先级,才进行
4444
运算;当后两种情况不满足时退出while,压入运算符栈
4545
4. 最后检查运算符栈是否还有元素
4646
5.输出数字栈栈顶,即结果
@@ -85,7 +85,7 @@ else if (c == ')') {
8585
} else {
8686
// 第四种情况,运算符,运算符栈不空并且栈顶不是(,栈顶优先级大于当前运算符优先级,就进行运算
8787
while (!op.isEmpty() && op.peek() != '(' && pr.get(op.peek()) >= pr.get(c))
88-
eval();
88+
eval(); //先处理op栈顶优先级大的运算
8989
//直接操作到上面三个条件中的后两个条件为止,然后把这个运算符压入运算符栈
9090
op.push(c);
9191
}

Algorithm/src/offer/_55_1TheDepthOfTheBinaryTree.java

+73
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,77 @@ public int maxDepth(TreeNode root) {
5757
return Math.max( maxDepth(root.left) , maxDepth(root.right) ) + 1;
5858
}
5959
}
60+
61+
/*
62+
* public class HashMapInitializeDemo {
63+
64+
/1**
65+
* HashMap初始化 使用匿名内部类的方式
66+
*1/
67+
public static void initialize() {
68+
HashMap<String, Integer> map = new HashMap<String, Integer>() {{
69+
put("a", 1);
70+
put("b", 2);
71+
put("c", 3);
72+
}};
73+
}
74+
75+
static class Num {
76+
77+
public Num() {
78+
79+
System.out.println("initialize num");
80+
}
81+
82+
void put() {
83+
System.out.println("put something");
84+
85+
}
86+
}
87+
88+
public static void main(String[] args) {
89+
//这是匿名内部类的写法,第一个大括号是代表类,第二个大括号代表匿名内部类的初始化代码块。
90+
这种语法表示该匿名内部类继承了Num类,还有我们经常使用匿名内部类来作为接口的实现类,作为方法入参。
91+
92+
93+
// 1.匿名内部类不能是抽象类,因为系统在创建匿名内部类时,会立即创建匿名内部类的对象,因此不允许
94+
将匿名内部类定义为抽象类。
95+
96+
97+
// 2.匿名内部类不能定义构造器(Constructor),由于匿名内部类没有类名,所以无法定义构造器,但匿名
98+
构造类可以定义初始化块,可以通过实例初始化块来完成构造函数需要完成的部分。
99+
100+
Num num = new Num() {{
101+
put();
102+
}};
103+
104+
Num num1 = new Num() {{
105+
System.out.println("create num");
106+
}};
107+
108+
List<String> list = new LinkedList<String>() {{
109+
add("a");
110+
add("b");
111+
}};
112+
System.out.println(list.get(0));
113+
114+
List<String> list1 = new LinkedListAnonymity<>();
115+
System.out.println(list1.get(0));
116+
}
117+
118+
/1**
119+
* LinkedListAnonymity
120+
* new LinkedListAnonymity<>(); 等同于HashMapInitializeDemo中的匿名类 List<String> list = new LinkedList<String>(){{add("a");add("b");}};
121+
* @param <T>
122+
*1/
123+
static class LinkedListAnonymity<T> extends LinkedList<T> {
124+
public LinkedListAnonymity() {
125+
add((T) "a");
126+
add((T) "b");
127+
}
128+
}
129+
130+
}
131+
132+
*/
60133
}

Algorithm/src/offer/_64Request.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package offer;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-07-11-17:32
6+
* @Function Description :64. 求1+2+…+n
7+
*/
8+
public class _64Request {
9+
class Solution {
10+
int res = 0 ;
11+
public int sumNums(int n) {
12+
/*需要实现 “当 n = 1n=1 时终止递归” 的需求,可通过短路效应实现.
13+
因为正常情况下递归需要结束条件用到if,但是本题禁止。。。
14+
当 n = 1 时 n > 1 不成立 ,此时 “短路” ,终止后续递归.
15+
为构成语句,需加一个辅助布尔量 x ,否则会报错;其余并无别的意思
16+
*/
17+
boolean x = n - 1 > 0 && sumNums( n - 1 ) > 0 ;
18+
res += n ;
19+
return res;
20+
}
21+
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package offer;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-07-11-18:02
6+
* @Function Description : 68.1 二叉搜索树的最近公共祖先
7+
*/
8+
public class _68_1TheRecentPublicAncestorOfTheBinarySearchTree {
9+
/**
10+
* Definition for a binary tree node.
11+
* public class TreeNode {
12+
* int val;
13+
* TreeNode left;
14+
* TreeNode right;
15+
* TreeNode(int x) { val = x; }
16+
* }
17+
*/
18+
class Solution {
19+
/*解题思路:
20+
① 树为 二叉搜索树 ,② 树的所有节点的值都是 唯一 的。根据以上条件,可方便地判断 p,q 与 root 的子树关系,即:
21+
若 root.val < p.val ,则 p 在 root 右子树 中;
22+
若 root.val > p.val ,则 p 在 root 左子树 中;
23+
若 root.val = p.val ,则 p 和 root 指向 同一节点 。
24+
25+
*/
26+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
27+
// 始终保持p结点的值小于q,方便下面while遍历时不用写判断
28+
if( p.val > q.val ){
29+
TreeNode temp = p ;
30+
p = q;
31+
q = temp;
32+
}
33+
while( root != null ){
34+
// pq都在root结点的左子树
35+
if( root.val > q.val )
36+
root = root.left;
37+
// pq都在root结点的右子树
38+
else if( root.val < p.val )
39+
root = root.right;
40+
else break;//找到
41+
}
42+
return root;
43+
}
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package offer;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-07-12-1:04
6+
* @Function Description :68.2 二叉树的最近公共祖先
7+
*/
8+
public class _68_2TheRecentPublicAncestorOfTheBinaryTree {
9+
/**
10+
* Definition for a binary tree node.
11+
* public class TreeNode {
12+
* int val;
13+
* TreeNode left;
14+
* TreeNode right;
15+
* TreeNode(int x) { val = x; }
16+
* }
17+
*/
18+
class Solution {
19+
// 本题是一个普通的二叉树,不是二叉搜索树!!!!!
20+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
21+
/*
22+
1. 当 left 和 right 同时为空 :说明 root 的左 / 右子树中都不包含p,q ,返回 null ;
23+
2.当 left 和 right 同时不为空 :说明 p,q 分列在 root 的 异侧 (分别在 左 / 右子树),因此 root为最近公共祖先,返回 rootroot ;
24+
3.当 left 为空 ,right 不为空 :p,q 都不在root 的左子树中,直接返回right 。具体可分为两种情况:
25+
3.1p,q 其中一个在 root 的 右子树 中,此时 right 指向 pp(假设为 p );
26+
3.2p,q 两节点都在 root的 右子树 中,此时的 right 指向 最近公共祖先节点 ;
27+
4.当 left 不为空 , right 为空 :与情况 3. 同理;
28+
观察发现, 情况 1. 可合并至 3. 和 4. 内,详见文章末尾代码。
29+
30+
31+
*/
32+
// 先序遍历
33+
// 终止条件:当越过叶节点,则直接返回 null ;当 root 等于 p, qp,q ,则直接返回 root ;
34+
35+
if( root == null || root == p || root == q ) return root;
36+
TreeNode left = lowestCommonAncestor( root.left , p , q );
37+
TreeNode right = lowestCommonAncestor( root.right , p , q );
38+
// 左树空,pq在右子树中
39+
if( left == null ) return right ;
40+
// 右树空,pq在左子树中
41+
if( right == null ) return left;
42+
return root;
43+
}
44+
45+
}
46+
}

0 commit comments

Comments
 (0)