File tree Expand file tree Collapse file tree 7 files changed +277
-0
lines changed Expand file tree Collapse file tree 7 files changed +277
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ ```
Original file line number Diff line number Diff line change
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
+ ```
Original file line number Diff line number Diff line change
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>> zigzagLevelOrder(TreeNode* root) {
14
+ vector<vector<int>> ret;
15
+ if(!root) return ret;
16
+
17
+ queue<TreeNode *> q;
18
+ q.push(root);
19
+ bool flag = true;
20
+
21
+ while(!q.empty())
22
+ {
23
+ int n = q.size();
24
+ vector<int> tmp;
25
+
26
+ while(n>0)
27
+ {
28
+ TreeNode *node = q.front();
29
+ q.pop();
30
+ n--;
31
+
32
+ tmp.push_back(node->val);
33
+ if(node->left) q.push(node->left);
34
+ if(node->right) q.push(node->right);
35
+
36
+ }
37
+
38
+ if(!flag)
39
+ reverse(tmp.begin(), tmp.end());
40
+ flag = !flag;
41
+ ret.push_back(tmp);
42
+ }
43
+
44
+ return ret;
45
+ }
46
+ };
47
+ ```
Original file line number Diff line number Diff line change
1
+ ...
2
+ class Solution {
3
+ public:
4
+ int firstUniqChar(string s) {
5
+ if(s.length() == 1)
6
+ return 0;
7
+ if(s.length() == 0)
8
+ return -1;
9
+
10
+ map<char, int> m;
11
+ int i = 0;
12
+ for(i=0; i<s.length(); i++)
13
+ {
14
+ m[ s[ i]] ++;
15
+ }
16
+
17
+ for(i=0; i<s.length(); i++)
18
+ {
19
+ if(m[ s[ i]] == 1)
20
+ break;
21
+ }
22
+
23
+ if(i == s.length())
24
+ return -1;
25
+ else
26
+ return i;
27
+ }
28
+ };
29
+ ...
Original file line number Diff line number Diff line change
1
+ ...
2
+ class Solution {
3
+ public:
4
+ string reverseWords(string s) {
5
+ string ret;
6
+
7
+ int cur = 0;
8
+ while(cur < s.length())
9
+ {
10
+ int begin = cur;
11
+ int end = s.find(" ", begin);
12
+ if(end == s.npos)
13
+ end = s.length();
14
+
15
+ string tmp = s.substr(begin, end-begin);
16
+ reversOne(tmp);
17
+ if(end != s.length())
18
+ ret = ret + tmp + " " ;
19
+ else
20
+ ret = ret + tmp;
21
+
22
+ cur = end+1;
23
+ }
24
+
25
+ return ret;
26
+ }
27
+
28
+
29
+ void reversOne(string &s)
30
+ {
31
+ int n = s.length();
32
+
33
+ int i = 0;
34
+ int j = n-1;
35
+ while(i < j)
36
+ {
37
+ swap(s[ i] , s[ j] );
38
+ i++;
39
+ j--;
40
+ }
41
+
42
+ }
43
+ };
44
+ ...
Original file line number Diff line number Diff line change
1
+ ```
2
+
3
+
4
+ class Solution {
5
+ public:
6
+ int lengthOfLastWord(string s) {
7
+ int end = s.length()-1;
8
+ while(s[end] == ' ')
9
+ end--;
10
+
11
+ int ret = 0;
12
+ while(end>=0 && s[end]!=' ')
13
+ {
14
+ end--;
15
+ ret++;
16
+ }
17
+
18
+ return ret;
19
+ }
20
+ };
21
+
22
+
23
+ ```
You can’t perform that action at this time.
0 commit comments