Skip to content

Commit c86c092

Browse files
author
Longerhaha
authored
Merge pull request gzc426#20 from gzc426/master
同步更新乔戈里leetcode每日一题
2 parents 4c66626 + 9c8e491 commit c86c092

File tree

23 files changed

+707
-1
lines changed

23 files changed

+707
-1
lines changed

2018.11.27-leetcode125/。。。.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
```
12
public boolean isPalindrome(String s){
23
if (s.length() >= 2){
34
int left = 0,right = s.length()-1;
@@ -26,3 +27,4 @@ public boolean isPalindrome(String s){
2627
}
2728
return true;
2829
}
30+
```

2018.11.28-leetcode151/。。。.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
```
12
public String reverseWords(String s) {
23
int i = s.length()-1,wordRight=-1;
34
StringBuilder builder = new StringBuilder();
@@ -25,3 +26,4 @@ public String reverseWords(String s) {
2526
}
2627
return builder.toString();
2728
}
29+
```

2018.11.29-leetcode443/。。。.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
public int compress(char[] chars) {
1+
```
2+
public int compress(char[] chars) {
23
int cur = 1;//压缩后字符串的长度
34
int repet = 1;
45
for (int i = 1; i < chars.length; i++){
@@ -24,3 +25,4 @@
2425
Log.d(new String(chars));
2526
return cur;
2627
}
28+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
leetcode of 101
2+
```
3+
/**
4+
* Definition for a binary tree node.
5+
* struct TreeNode {
6+
* int val;
7+
* TreeNode *left;
8+
* TreeNode *right;
9+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
bool isSymmetric(TreeNode* p1,TreeNode * p2){
15+
if((p1 == nullptr && p2!=nullptr) || (p2 == nullptr && p1!=nullptr)) return false;
16+
if(p1 == nullptr && p2 == nullptr) return true;
17+
if(p1->val != p2->val) return false;
18+
return isSymmetric(p1->left,p2->right) && isSymmetric(p1->right,p2->left);
19+
20+
}
21+
bool isSymmetric(TreeNode* root) {
22+
return isSymmetric(root,root);
23+
}
24+
};
25+
```

2018.12.04-leetcode101/sourcema.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# LeetCode 101
2+
/**
3+
* Definition for a binary tree node.
4+
* public class TreeNode {
5+
* int val;
6+
* TreeNode left;
7+
* TreeNode right;
8+
* TreeNode(int x) { val = x; }
9+
* }
10+
*/
11+
class Solution {
12+
public boolean isSymmetric(TreeNode root) {
13+
if (root == null) {
14+
return true;
15+
}
16+
return symmetric(root.left, root.right);
17+
}
18+
private boolean symmetric(TreeNode p, TreeNode q) {
19+
if (p == null && q == null) {
20+
return true;
21+
}
22+
if ((p == null && q != null) || (p != null && q == null) || p.val != q.val) {
23+
return false;
24+
}
25+
return symmetric(p.left, q.right) && symmetric(p.right, q.left);
26+
}
27+
}

2018.12.04-leetcode101/啦啦啦.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
```java
2+
/**
3+
* Definition for a binary tree node.
4+
* public class TreeNode {
5+
* int val;
6+
* TreeNode left;
7+
* TreeNode right;
8+
* TreeNode(int x) { val = x; }
9+
* }
10+
*/
11+
class Solution {
12+
public boolean isSymmetric(TreeNode root) {
13+
return cal(root,root);
14+
}
15+
16+
boolean cal(TreeNode r1,TreeNode r2){
17+
if(r1==null&&r2==null)return true;
18+
if(r1==null||r2==null)return false;
19+
return r1.val==r2.val&&cal(r1.left,r2.right)&&cal(r1.right,r2.left);
20+
}
21+
}
22+
```

2018.12.04-leetcode101/李碧涵.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
### [101. Symmetric Tree](https://leetcode.com/problems/symmetric-tree/description/)
2+
**题目描述**
3+
> 判断是否是镜像二叉树(对称)
4+
5+
**例子**
6+
7+
8+
**思想**
9+
(递归)
10+
定义一辅助函数,输入为left和right,判断是否相等。
11+
(迭代)
12+
定义两个栈,层次遍历时判断,并分别从左向右和从右向左存储。
13+
14+
**解法1**
15+
递归
16+
```python
17+
# Definition for a binary tree node.
18+
# class TreeNode(object):
19+
# def __init__(self, x):
20+
# self.val = x
21+
# self.left = None
22+
# self.right = None
23+
24+
class Solution(object):
25+
def isSymmetric(self, root):
26+
"""
27+
:type root: TreeNode
28+
:rtype: bool
29+
"""
30+
if not root:
31+
return True
32+
return self.helper(root.left, root.right)
33+
34+
def helper(self, left, right):
35+
if not left and not right:
36+
return True
37+
if not left or not right or left.val != right.val:
38+
return False
39+
return self.helper(left.left, right.right) and self.helper(left.right, right.left)
40+
```
41+
**解法2**
42+
迭代
43+
```python
44+
# Definition for a binary tree node.
45+
# class TreeNode(object):
46+
# def __init__(self, x):
47+
# self.val = x
48+
# self.left = None
49+
# self.right = None
50+
51+
class Solution(object):
52+
def isSymmetric(self, root):
53+
"""
54+
:type root: TreeNode
55+
:rtype: bool
56+
"""
57+
if not root or not root.left and not root.right:
58+
return True
59+
if not root.left or not root.right:
60+
return False
61+
62+
queue1, queue2 = [root.left], [root.right]
63+
while queue1 and queue2:
64+
node1 = queue1.pop(0)
65+
node2 = queue2.pop(0)
66+
67+
if node1.val != node2.val:
68+
return False
69+
70+
if node1.left and node2.right:
71+
queue1.append(node1.left)
72+
queue2.append(node2.right)
73+
elif node1.left or node2.right:
74+
return False
75+
76+
if node1.right and node2.left:
77+
queue1.append(node1.right)
78+
queue2.append(node2.left)
79+
elif node1.right or node2.left:
80+
return False
81+
return True
82+
```

2018.12.04-leetcode101/杨.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
```
2+
/**
3+
* 如果一个树的左子树与右子树镜像对称,那么这个树是对称的。
4+
* 如果同时满足下面的条件,两个树互为镜像:
5+
* 1.它们的两个根结点具有相同的值。
6+
* 2.每个树的右子树都与另一个树的左子树镜像对称。
7+
*/
8+
public boolean isSymmetric(TreeNode root) {
9+
if(root == null ){
10+
return true;
11+
}
12+
return isMirrorSymmetry(root.left,root.right);
13+
}
14+
15+
public boolean isMirrorSymmetry(TreeNode left,TreeNode right){
16+
if(left==null && right==null){
17+
return true;
18+
}
19+
if(left==null || right==null){
20+
return false;
21+
}
22+
return left.val==right.val && isMirrorSymmetry(left.left,right.right) && isMirrorSymmetry(left.right,right.left);
23+
}
24+
25+
```

2018.12.05-leetcode102/GatesMa.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# C++
2+
```cpp
3+
class Solution {
4+
public:
5+
vector<vector<int>> res;
6+
void check(TreeNode* root, int depth){
7+
if(root == NULL) return ;
8+
if(res.size() == depth)
9+
{
10+
res.push_back(vector<int>());
11+
}
12+
res[depth].push_back(root->val);
13+
check(root->left, depth + 1);
14+
check(root->right, depth + 1);
15+
}
16+
vector<vector<int>> levelOrder(TreeNode* root) {
17+
check(root, 0);
18+
return res;
19+
}
20+
};
21+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
leetcode of 102
2+
```
3+
/**
4+
* Definition for a binary tree node.
5+
* struct TreeNode {
6+
* int val;
7+
* TreeNode *left;
8+
* TreeNode *right;
9+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
10+
* };
11+
*/
12+
struct DataNode{
13+
TreeNode * p;
14+
int depth;
15+
};
16+
class Solution {
17+
public:
18+
vector<vector<int>> levelOrder(TreeNode* root) {
19+
vector<vector<int>> ans;
20+
if(root == nullptr) return ans;
21+
queue<DataNode> q;
22+
int depth = 0;
23+
q.push(DataNode{root,depth});
24+
vector<int>t;
25+
while(!q.empty()){
26+
DataNode dn = q.front();
27+
q.pop();
28+
if(depth == dn.depth){
29+
t.push_back(dn.p->val);
30+
}else{
31+
depth = dn.depth;
32+
ans.push_back(t);
33+
t.clear();
34+
t.push_back(dn.p->val);
35+
}
36+
if(dn.p->left != nullptr)
37+
q.push(DataNode{dn.p->left,depth+1});
38+
if(dn.p->right != nullptr)
39+
q.push(DataNode{dn.p->right,depth+1});
40+
41+
}
42+
ans.push_back(t);
43+
return ans;
44+
45+
}
46+
};
47+
```

0 commit comments

Comments
 (0)