Skip to content

Commit 52a7307

Browse files
authored
Merge pull request gzc426#20 from gzc426/master
拉取最新
2 parents 4fdd5aa + 7fb0cf7 commit 52a7307

34 files changed

+1412
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public:
3+
vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
4+
vector<string> ret;
5+
6+
int size = words.size();
7+
int n = pattern.length();
8+
9+
for(int i=0; i<size; i++)
10+
{
11+
map<char, char> m;
12+
map<char, char> m1;
13+
int j = 0;
14+
for(j=0; j<n; j++)
15+
{
16+
if(m.find(pattern[j])==m.end())
17+
{
18+
m[pattern[j]] = words[i][j];
19+
}
20+
21+
if(m1.find(words[i][j])==m1.end())
22+
{
23+
m1[words[i][j]] = pattern[j];
24+
}
25+
26+
if(m.find(pattern[j])!=m.end() && m[pattern[j]] != words[i][j])
27+
break;
28+
29+
if(m1.find(words[i][j])!=m.end() && m1[words[i][j]]!=pattern[j])
30+
break;
31+
32+
}
33+
34+
if(j == n)
35+
ret.push_back(words[i]);
36+
}
37+
38+
return ret;
39+
}
40+
};

2018.12.04-leetcode101/MQQM.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
题目:
3+
给定一个二叉树,检查它是否是镜像对称的。
4+
5+
做法:
6+
给定对应位置上的两个节点,递归判断这两个节点是否一致。
7+
8+
参考:
9+
https://www.cnblogs.com/xiaozhuyang/p/7376749.html
10+
*/
11+
12+
/**
13+
* Definition for a binary tree node.
14+
* struct TreeNode {
15+
* int val;
16+
* TreeNode *left;
17+
* TreeNode *right;
18+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
19+
* };
20+
*/
21+
class Solution {
22+
public:
23+
bool isSymmetric(TreeNode* root) {
24+
if( root == NULL ){
25+
return true;
26+
}
27+
28+
return isEqual(root->left, root->right);
29+
}
30+
bool isEqual(TreeNode* left, TreeNode* right){//给定的两个节点已经是对应位置上的节点
31+
if( left == NULL && right == NULL ){//两个节点都是空
32+
return true;
33+
}
34+
if( left == NULL || right == NULL ){//有一个节点是空
35+
return false;
36+
}
37+
if( left->val != right->val ){//两个节点都存在,但是不相同
38+
return false;
39+
}
40+
41+
return isEqual(left->left, right->right) && isEqual(left->right, right->left);
42+
}
43+
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
```
2+
/**
3+
* Definition for a binary tree node.
4+
* struct TreeNode {
5+
* int val;
6+
* TreeNode *left;
7+
* TreeNode *right;
8+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
bool isSymmetric(TreeNode* root) {
14+
if(!root) return true;
15+
16+
queue<TreeNode*> q1;
17+
queue<TreeNode*> q2;
18+
q1.push(root->left);
19+
q2.push(root->right);
20+
21+
22+
while(!q1.empty() && !q2.empty())
23+
{
24+
TreeNode *l = q1.front();
25+
TreeNode *r = q2.front();
26+
27+
q1.pop();
28+
q2.pop();
29+
30+
if( (!l&&r) || (l&&!r) ) return false;
31+
32+
if(l && r)
33+
{
34+
if(l->val != r->val)
35+
return false;
36+
37+
q1.push(l->left);
38+
q1.push(l->right);
39+
40+
q2.push(r->right);
41+
q2.push(r->left);
42+
}
43+
44+
}
45+
46+
return true;
47+
}
48+
};
49+
```

2018.12.05-leetcode102/MQQM.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
题目:
3+
给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。
4+
5+
做法:用队列。
6+
*/
7+
8+
/**
9+
* Definition for a binary tree node.
10+
* struct TreeNode {
11+
* int val;
12+
* TreeNode *left;
13+
* TreeNode *right;
14+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
15+
* };
16+
*/
17+
class Solution {
18+
public:
19+
vector<vector<int>> levelOrder(TreeNode* root) {
20+
vector<vector<int>> rst;
21+
if(root == NULL){
22+
return rst;
23+
}
24+
25+
queue<TreeNode*> que;
26+
que.push(root);
27+
while( !que.empty() ){
28+
int len=que.size();
29+
30+
vector<int> levelvec;
31+
for(int i=0; i<len; i++){
32+
TreeNode* p=que.front();
33+
que.pop();
34+
levelvec.push_back(p->val);
35+
36+
if(p->left != NULL){
37+
que.push(p->left);
38+
}
39+
if(p->right != NULL){
40+
que.push(p->right);
41+
}
42+
}
43+
rst.push_back(levelvec);
44+
}
45+
46+
return rst;
47+
}
48+
};

2018.12.05-leetcode102/kaizen

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @author kaizen
3+
* @since 2018-12-07
4+
**/
5+
public class TreeNode {
6+
int val;
7+
TreeNode left;
8+
TreeNode right;
9+
TreeNode(int x) { val = x; }
10+
11+
class Solution {
12+
13+
14+
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
15+
if(root == null)
16+
return new ArrayList<>();
17+
List<List<Integer>> res = new ArrayList<>();
18+
Queue<TreeNode> queue = new LinkedList<TreeNode>();
19+
queue.add(root);
20+
while(!queue.isEmpty()){
21+
int count = queue.size();
22+
List<Integer> list = new ArrayList<Integer>();
23+
while(count > 0){
24+
TreeNode node = queue.poll();
25+
list.add(node.val);
26+
if(node.left != null)
27+
queue.add(node.left);
28+
if(node.right != null)
29+
queue.add(node.right);
30+
count--;
31+
}
32+
res.add(list);
33+
}
34+
return res;
35+
}
36+
37+
}
38+
39+
40+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
```
2+
/**
3+
* Definition for a binary tree node.
4+
* struct TreeNode {
5+
* int val;
6+
* TreeNode *left;
7+
* TreeNode *right;
8+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
vector<vector<int>> levelOrder(TreeNode* root) {
14+
vector<vector<int>> ret;
15+
if(!root) return ret;
16+
17+
queue<TreeNode*> q;
18+
q.push(root);
19+
20+
while(!q.empty())
21+
{
22+
int n = q.size();
23+
vector<int> tmp;
24+
25+
while(n>0)
26+
{
27+
TreeNode *node = q.front();
28+
q.pop();
29+
n--;
30+
31+
tmp.push_back(node->val);
32+
if(node->left)
33+
q.push(node->left);
34+
if(node->right)
35+
q.push(node->right);
36+
37+
}
38+
39+
ret.push_back(tmp);
40+
}
41+
42+
return ret;
43+
}
44+
};
45+
```

0 commit comments

Comments
 (0)