Skip to content

Commit 48cdf71

Browse files
committed
Java
1 parent 49f7912 commit 48cdf71

2 files changed

Lines changed: 60 additions & 2 deletions

File tree

‎docs/AimForOffer/5_树.md‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ private int height(TreeNode root){
511511

512512

513513

514-
## 16、二叉树的最近公共祖先
514+
## *16、二叉树的最近公共祖先
515515

516516
[二叉树的最近公共祖先](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/)
517517

@@ -528,7 +528,7 @@ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
528528
return root;
529529
}
530530

531-
//判断已 root 为根节点二叉树是否包含 p 节点
531+
//判断以root 为根节点二叉树是否包含 p 节点
532532
private boolean contains(TreeNode root,TreeNode p){
533533
if(root==null || p==null){
534534
return false;
@@ -550,6 +550,12 @@ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
550550
if(p== root || q==root){
551551
return root;
552552
}
553+
554+
// 1
555+
// / \
556+
// 2 3
557+
// 其中 p 为 2,q 为 3 则 left=p,right=q
558+
//显然 root 为 p、q 的最近公共祖先
553559
//p,q 在左子树中是否有公共父节点
554560
TreeNode left = lowestCommonAncestor(root.left,p,q);
555561
//p,q 在右子树中是否有公共父节点

‎docs/AimForOffer/8_动态规划.md‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,59 @@
55
[正则表达式匹配](https://www.nowcoder.com/practice/45327ae22b7b413ea21df13ee7d6429c?tpId=13&tqId=11205&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
66

77
```java
8+
//思路:
9+
// 当模式中的第二个字符不是“*”时:
10+
// 1、如果字符串第一个字符和模式中的第一个字符相匹配,那么字符串和模式都后移一个字符,然后匹配剩余的。
11+
// 2、如果字符串第一个字符和模式中的第一个字符相不匹配,直接返回false。
12+
//
13+
// 当模式中的第二个字符是“*”时:
14+
// 如果字符串第一个字符跟模式第一个字符不匹配,则模式后移2个字符,继续匹配。
15+
// 如果字符串第一个字符跟模式第一个字符匹配,可以有3种匹配方式:
16+
// 1、模式后移2字符,相当于x*被忽略;
17+
// 2、字符串后移1字符,模式后移2字符;(匹配一位)
18+
// 3、字符串后移1字符,模式不变,即继续匹配字符下一位,因为*可以匹配多位;
19+
20+
public boolean match(char[] str, char[] pattern) {
21+
if(str==null || pattern==null){
22+
return false;
23+
}
24+
return match(str,0,pattern,0);
25+
}
26+
27+
private boolean match(char[] str,int strIndex,char[] pattern,int patternIndex){
28+
if(strIndex==str.length && patternIndex==pattern.length){
29+
// str 和 pattern 都到达了末尾,则返回 true
30+
return true;
31+
}
32+
if(patternIndex==pattern.length && strIndex!=str.length){
33+
//pattern 先到达了末尾,则返回 false
34+
return false;
35+
}
836

37+
//当模式中的第二个字符不是“*”时:
38+
if(patternIndex+1<pattern.length && pattern[patternIndex+1]=='*'){
39+
if ((strIndex != str.length && pattern[patternIndex] == str[strIndex]) ||
40+
(strIndex != str.length && pattern[patternIndex] == '.')){
41+
return match(str,strIndex,pattern,patternIndex+2) ||
42+
//1、模式后移2字符,相当于x*被忽略;
43+
match(str,strIndex+1,pattern,patternIndex+2) ||
44+
// 2、字符串后移1字符,模式后移2字符;(匹配一位)
45+
match(str,strIndex+1,pattern,patternIndex);
46+
// 3、字符串后移1字符,模式不变,即继续匹配字符下一位,因为*可以匹配多位
47+
}else{ // 前一个元素既不相等,也不是"*",模式后移 2 字符,相当于 x* 被忽略。
48+
return match(str,strIndex,pattern,patternIndex+2);
49+
}
50+
}else{
51+
// 如果字符串第一个字符和模式中的第一个字符相匹配,
52+
// 那么字符串和模式都后移一个字符,然后匹配剩余的。
53+
if((strIndex != str.length && pattern[patternIndex] == str[strIndex]) ||
54+
(pattern[patternIndex] == '.' && strIndex != str.length)){
55+
return match(str,strIndex+1,pattern,patternIndex+1);
56+
}else{
57+
return false;
58+
}
59+
}
60+
}
961
```
1062

1163

0 commit comments

Comments
 (0)