Skip to content

Commit 40a8653

Browse files
committed
add 13-17
1 parent fd42f7c commit 40a8653

File tree

6 files changed

+215
-1
lines changed

6 files changed

+215
-1
lines changed

ClimbingStairs.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
You are climbing a stair case. It takes n steps to reach to the top.
3+
4+
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
5+
*/
6+
7+
class Solution {
8+
public:
9+
int climbStairs(int n) {
10+
if(n < 1)
11+
return 0;
12+
int dp[n];
13+
dp[0] = 1; // first stair
14+
dp[1] = 2; // second stair
15+
for(int i = 2; i < n; ++i)
16+
dp[i] = dp[i-1] + dp[i-2];
17+
return dp[n-1];
18+
}
19+
};

MergeSortedArray.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Given two sorted integer arrays A and B, merge B into A as one sorted array.
3+
4+
Note:
5+
You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.
6+
*/
7+
8+
class Solution {
9+
public:
10+
void merge(int A[], int m, int B[], int n) {
11+
int k = m + n - 1;
12+
while(m > 0 && n > 0)
13+
{
14+
if(A[m-1] > B[n-1])
15+
{
16+
A[k] = A[m-1];
17+
--m;
18+
}
19+
else
20+
{
21+
A[k] = B[n-1];
22+
--n;
23+
}
24+
--k;
25+
}
26+
while(n > 0)
27+
{
28+
A[k] = B[n-1];
29+
--k;
30+
--n;
31+
}
32+
return;
33+
}
34+
};

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,25 @@ Solve problems in LeetCode http://oj.leetcode.com/.
7171

7272
Use FINITE AUTOMATA: How to construct the state transition graph correctly?
7373

74-
Reference: http://discuss.leetcode.com/questions/241/valid-number/768
74+
Reference: http://discuss.leetcode.com/questions/241/valid-number/768
75+
76+
12. Clibming Stairs
77+
78+
You have two choices, 1 step or 2 steps, dp[i] = dp[i-1] + dp[i-2].
79+
80+
13. Set Matrix Zeroes
81+
82+
Save row and column index firstly, then set 0s according to the index we saved.
83+
84+
14. Merge Sorted Array
85+
86+
Backward to stoer the larger number from A and B. Only move the rest numbers in B to A if B has numbers left after A has been moved.
87+
88+
15. Valid Palindrome
89+
90+
isalnum(c): Checks whether c is either a decimal digit or an uppercase or lowercase letter.
91+
92+
16. Word Ladder
93+
94+
Graph BFS problem. Use a queue and a visited array for BFS without constructing the graph.
95+

SetMatrixZeroes.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
3+
4+
Follow up:
5+
Did you use extra space?
6+
A straight forward solution using O(mn) space is probably a bad idea.
7+
A simple improvement uses O(m + n) space, but still not the best solution.
8+
Could you devise a constant space solution?
9+
*/
10+
11+
class Solution {
12+
public:
13+
void setZeroes(vector<vector<int> > &matrix) {
14+
if(matrix.empty())
15+
return;
16+
int m = matrix.size(), n = matrix[0].size();
17+
vector<bool> row(m, false), col(n, false);
18+
for(int i = 0; i < m; ++i)
19+
for(int j = 0; j < n; ++j)
20+
if(matrix[i][j] == 0)
21+
{
22+
row[i] = true;
23+
col[j] = true;
24+
}
25+
for(int i = 0; i < m; ++i)
26+
for(int j = 0; j < n; ++j)
27+
if(row[i] || col[j])
28+
matrix[i][j] = 0;
29+
return;
30+
}
31+
};

ValidPalindrome.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
3+
4+
For example,
5+
"A man, a plan, a canal: Panama" is a palindrome.
6+
"race a car" is not a palindrome.
7+
8+
Note:
9+
Have you consider that the string might be empty? This is a good question to ask during an interview.
10+
11+
For the purpose of this problem, we define empty string as valid palindrome.
12+
*/
13+
14+
class Solution {
15+
public:
16+
bool isPalindrome1(string s) {
17+
if(s.empty())
18+
return true;
19+
int l = 0, r = s.size()-1;
20+
while(l < r)
21+
{
22+
while(l < r && (!isalpha(s[l]) && !isdigit(s[l])))
23+
++l;
24+
while(l < r && (!isalpha(s[r]) && !isdigit(s[r])))
25+
--r;
26+
if(l >= r)
27+
return true;
28+
if(tolower(s[l]) != tolower(s[r]))
29+
return false;
30+
++l;
31+
--r;
32+
}
33+
return true;
34+
}
35+
};
36+
37+
class Solution {
38+
public:
39+
bool isPalindrome2(string s) {
40+
for (int i = 0, j = s.size() - 1; i < j; ++i, --j)
41+
{
42+
while (i < j && !isalnum(s[i])) i++;
43+
while (i < j && !isalnum(s[j])) j--;
44+
45+
if (tolower(s[i]) != tolower(s[j]))
46+
return false;
47+
}
48+
return true;
49+
}
50+
};

WordLadder.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:
3+
4+
Only one letter can be changed at a time
5+
Each intermediate word must exist in the dictionary
6+
For example,
7+
8+
Given:
9+
start = "hit"
10+
end = "cog"
11+
dict = ["hot","dot","dog","lot","log"]
12+
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
13+
return its length 5.
14+
15+
Note:
16+
Return 0 if there is no such transformation sequence.
17+
All words have the same length.
18+
All words contain only lowercase alphabetic characters.
19+
*/
20+
21+
class Solution {
22+
public:
23+
int ladderLength(string start, string end, unordered_set<string> &dict) {
24+
int length = 2, count = 1, neighb = 0;
25+
unordered_set<string> visited;
26+
queue<string> level;
27+
level.push(start);
28+
visited.insert(start);
29+
while(!level.empty())
30+
{
31+
string curr = level.front();
32+
level.pop();
33+
--count;
34+
for(int i = 0; i < curr.size(); ++i)
35+
{
36+
for(int j = 0; j < 26; ++j)
37+
{
38+
string next = curr;
39+
next[i] = 'a' + j;
40+
if(next == end)
41+
return length;
42+
if(dict.find(next) != dict.end() && visited.find(next) == visited.end())
43+
{
44+
++neighb;
45+
level.push(next);
46+
visited.insert(next);
47+
}
48+
}
49+
}
50+
if(count == 0) // new level starts
51+
{
52+
++length;
53+
count = neighb;
54+
neighb = 0;
55+
}
56+
}
57+
return 0;
58+
}
59+
};

0 commit comments

Comments
 (0)