Skip to content

Commit bf29891

Browse files
committed
add 31-34
1 parent 4b8e186 commit bf29891

File tree

5 files changed

+219
-9
lines changed

5 files changed

+219
-9
lines changed

BinaryTreeLevelOrderTraversal.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
3+
4+
For example:
5+
Given binary tree {3,9,20,#,#,15,7},
6+
3
7+
/ \
8+
9 20
9+
/ \
10+
15 7
11+
return its level order traversal as:
12+
[
13+
[3],
14+
[9,20],
15+
[15,7]
16+
]
17+
*/
18+
19+
/**
20+
* Definition for binary tree
21+
* struct TreeNode {
22+
* int val;
23+
* TreeNode *left;
24+
* TreeNode *right;
25+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
26+
* };
27+
*/
28+
class Solution {
29+
public:
30+
vector<vector<int> > levelOrder(TreeNode *root) {
31+
vector<vector<int> > orders;
32+
if(!root)
33+
return orders;
34+
queue<TreeNode *> level;
35+
vector<int> lv;
36+
level.push(root);
37+
int count = 1, ncount = 0;
38+
while(!level.empty())
39+
{
40+
TreeNode *node = level.front();
41+
level.pop();
42+
lv.push_back(node->val);
43+
--count;
44+
if(node->left)
45+
{
46+
++ncount;
47+
level.push(node->left);
48+
}
49+
if(node->right)
50+
{
51+
++ncount;
52+
level.push(node->right);
53+
}
54+
if(count == 0)
55+
{
56+
orders.push_back(lv);
57+
lv.clear();
58+
count = ncount;
59+
ncount = 0;
60+
}
61+
}
62+
return orders;
63+
}
64+
};

DecodeWays.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
A message containing letters from A-Z is being encoded to numbers using the following mapping:
3+
4+
'A' -> 1
5+
'B' -> 2
6+
...
7+
'Z' -> 26
8+
Given an encoded message containing digits, determine the total number of ways to decode it.
9+
10+
For example,
11+
Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).
12+
13+
The number of ways decoding "12" is 2.
14+
*/
15+
class Solution {
16+
public:
17+
int numDecodings(string s) {
18+
if(s.empty() || s == "0")
19+
return 0;
20+
vector<int> ways(s.size()+1, 1);
21+
for(int i = s.size()-1; i >= 0; --i)
22+
{
23+
if(s[i] == '0')
24+
ways[i] = 0;
25+
else
26+
ways[i] = ways[i+1];
27+
if(i+1 < s.size() && ((s[i] == '1') || (s[i] == '2' && s[i+1] - '0' <= 6)))
28+
ways[i] += ways[i+2];
29+
}
30+
return ways[0];
31+
}
32+
};

README.md

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Solve problems in LeetCode http://oj.leetcode.com/.
1515

1616
Use long long to make sure the string can be converted, then check whether it exceeds INT_MAX/INT_MIN.
1717

18-
3. 3 Sum
18+
**3. 3 Sum**
1919

2020
Size of vector >= 3
2121

@@ -31,7 +31,7 @@ Solve problems in LeetCode http://oj.leetcode.com/.
3131

3232
Use a dummy node, link the next to the new Listnode with the smaller value, then return dummy->next.
3333

34-
6. Implement strStr()
34+
**6. Implement strStr()**
3535

3636
Use char* directly to compare.
3737

@@ -51,15 +51,15 @@ Solve problems in LeetCode http://oj.leetcode.com/.
5151

5252
Two pointers, one faster, one slow, then meet together or meet NULL.
5353

54-
10. Insert Interval
54+
**10. Insert Interval**
5555

5656
Usefull of vector insert() and erase().
5757

5858
Insert: inserting new elements before the element at the specified position.
5959

6060
Erase return value: An iterator pointing to the new location of the element that followed the last element erased by the function call.
6161

62-
11. Valid Number
62+
**11. Valid Number**
6363

6464
Too many different cases to handle.
6565

@@ -89,11 +89,11 @@ Solve problems in LeetCode http://oj.leetcode.com/.
8989

9090
Recursive Problem. Left nodes should locate in [min, parent->val], right nodes should locate in [parent->val, max].
9191

92-
16. Valid Palindrome
92+
**16. Valid Palindrome**
9393

9494
isalnum(c): Checks whether c is either a decimal digit or an uppercase or lowercase letter.
9595

96-
17. Word Ladder
96+
**17. Word Ladder**
9797

9898
Graph BFS problem. Use a queue and a visited array for BFS without constructing the graph.
9999

@@ -103,7 +103,7 @@ Solve problems in LeetCode http://oj.leetcode.com/.
103103

104104
Stop condition: while(l1 || l2 || carry)
105105

106-
19. Integer to Roman
106+
**19. Integer to Roman**
107107

108108
Left(-) and right(+) for 5s and 10s.
109109

@@ -121,7 +121,7 @@ Solve problems in LeetCode http://oj.leetcode.com/.
121121

122122
Make sure number of left parentheses is not smaller than number of right parentheses.
123123

124-
23. Merge k Sorted Lists
124+
**23. Merge k Sorted Lists**
125125

126126
Call the Merge Tow Sorted Lists k times, you are done!
127127

@@ -150,10 +150,30 @@ Solve problems in LeetCode http://oj.leetcode.com/.
150150

151151
Similar to Add Two Numbers. Use string instead of linked list.
152152

153-
29. Sqrt(x)
153+
**29. Sqrt(x)**
154154

155155
Binary search method. Be careful about overflow, using long long.
156156

157157
30. Combinations
158158

159159
DFS. In position k, iterative through k+1 to n.
160+
161+
31. Subsets
162+
163+
DFS. Similar to Combinations, but stores all subsets into result.
164+
165+
**32. Word Search**
166+
167+
DFS. If current char match the word, traversal its four directions. Use a visited[][] to mark visited or not.
168+
169+
Put the check into a function for a better structure of code.
170+
171+
**33. Decode Ways**
172+
173+
Dynamic problem. Similar to Climbing Stairs, with some limitations.
174+
175+
dp[i] = dp[i-1] + dp[i-2], add dp[i-2] if two char can be put together.
176+
34. Binary Tree Level Order Traversal
177+
178+
Use a queue to store the children nodes of current level.
179+

Subsets.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Given a set of distinct integers, S, return all possible subsets.
3+
4+
Note:
5+
Elements in a subset must be in non-descending order.
6+
The solution set must not contain duplicate subsets.
7+
For example,
8+
If S = [1,2,3], a solution is:
9+
10+
[
11+
[3],
12+
[1],
13+
[2],
14+
[1,2,3],
15+
[1,3],
16+
[2,3],
17+
[1,2],
18+
[]
19+
]
20+
*/
21+
22+
class Solution {
23+
public:
24+
vector<vector<int> > subsets(vector<int> &S) {
25+
vector<vector<int> > result;
26+
vector<int> sets;
27+
sort(S.begin(), S.end());
28+
setHelper(S, result, sets, 0);
29+
return result;
30+
}
31+
void setHelper(vector<int> &S, vector<vector<int> > &result, vector<int> &sets, int k)
32+
{
33+
result.push_back(sets);
34+
for(int i = k; i < S.size(); ++i)
35+
{
36+
sets.push_back(S[i]);
37+
setHelper(S, result, sets, i+1);
38+
sets.pop_back();
39+
}
40+
}
41+
};

WordSearch.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Given a 2D board and a word, find if the word exists in the grid.
3+
4+
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
5+
6+
For example,
7+
Given board =
8+
9+
[
10+
["ABCE"],
11+
["SFCS"],
12+
["ADEE"]
13+
]
14+
word = "ABCCED", -> returns true,
15+
word = "SEE", -> returns true,
16+
word = "ABCB", -> returns false.
17+
*/
18+
class Solution {
19+
public:
20+
bool exist(vector<vector<char> > &board, string word) {
21+
if(word.empty())
22+
return true;
23+
if(board.empty())
24+
return false;
25+
int m = board.size(), n = board[0].size();
26+
vector<vector<bool> > visited(m, vector<bool>(n, false));
27+
for(int i = 0; i < m; ++i)
28+
for(int j = 0; j < n; ++j)
29+
if(searchHelper(board, visited, word, 0, i, j))
30+
return true;
31+
return false;
32+
}
33+
bool searchHelper(vector<vector<char> > &board, vector<vector<bool> > &visited, string word, int l, int i, int j)
34+
{
35+
if(word.size() == l)
36+
return true;
37+
if(i < 0 || i >= board.size() || j < 0 || j >= board[0].size())
38+
return false;
39+
if(board[i][j] != word[l] || visited[i][j])
40+
return false;
41+
visited[i][j] = true;
42+
if(searchHelper(board, visited, word, l+1, i-1, j))
43+
return true;
44+
if(searchHelper(board, visited, word, l+1, i+1, j))
45+
return true;
46+
if(searchHelper(board, visited, word, l+1, i, j-1))
47+
return true;
48+
if(searchHelper(board, visited, word, l+1, i, j+1))
49+
return true;
50+
visited[i][j] = false;
51+
return false;
52+
}
53+
};

0 commit comments

Comments
 (0)