Skip to content

Commit bfc8da4

Browse files
committed
update 42, 43, 44, 45
1 parent 24d0373 commit bfc8da4

13 files changed

+348
-130
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* @Author: [email protected]
3+
* @Last Modified time: 2016-07-26 11:48:04
4+
*/
5+
6+
class Solution {
7+
public:
8+
/*
9+
Dynamic Programming
10+
11+
dp[i][j] represents isMatch(p[0...i-1], s[0...j-1]), default is False;
12+
dp[i][0]: isMatch(p[0...i], ""), dp[0][j]: isMatch("", s[0...j])
13+
dp[0][0] represents
14+
15+
If p[i] is "*", dp[i+1][j+1] =
16+
1. dp[i][j+1] # * matches 0 element in s;
17+
2. dp[i][j] # * matches 1 element in s;
18+
3. dp[i+1][j] # * matches more than one in s.
19+
*/
20+
bool isMatch(string s, string p) {
21+
int s_size = s.size();
22+
int p_size = p.size();
23+
if(s_size == 0){
24+
if(p.find_first_not_of('*') == string::npos){
25+
return true;
26+
}
27+
return false;
28+
}
29+
30+
int not_star_cnt = 0;
31+
for(int i=0; i<p_size; i++){
32+
if(p[i] != '*'){
33+
not_star_cnt += 1;
34+
}
35+
}
36+
if(not_star_cnt > s_size){
37+
return false;
38+
}
39+
40+
vector<vector<bool>> dp(p_size+1, vector<bool>(s_size+1, false));
41+
dp[0][0] = true;
42+
for(int i=0; i<p_size; i++){
43+
dp[i+1][0] = dp[i][0] && p[i] == '*';
44+
}
45+
46+
for(int i=0; i<p_size; i++){
47+
for(int j=0; j<s_size; j++){
48+
if(p[i] == '*'){
49+
dp[i+1][j+1] = dp[i][j] || dp[i][j+1] || dp[i+1][j];
50+
}
51+
else{
52+
dp[i+1][j+1] = dp[i][j] && (p[i] == s[j] || p[i] == '?');
53+
}
54+
}
55+
}
56+
return dp[p_size][s_size];
57+
}
58+
};
59+
60+
/*
61+
"aa"
62+
"a"
63+
"aa"
64+
"aa"
65+
"aaa"
66+
"aa"
67+
"aa"
68+
"*"
69+
"aa"
70+
"a*"
71+
"ab"
72+
"?*"
73+
"aab"
74+
"c*a*b"
75+
*/
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
class Solution(object):
6+
def isMatch(self, s, p):
7+
""" Dynamic Programming
8+
9+
dp[i][j] represents isMatch(p[0...i-1], s[0...j-1]), default is False;
10+
dp[i][0]: isMatch(p[0...i], ""), dp[0][j]: isMatch("", s[0...j])
11+
dp[0][0] represents
12+
13+
If p[i] is "*", dp[i+1][j+1] =
14+
1. dp[i][j+1] # * matches 0 element in s;
15+
2. dp[i][j] # * matches 1 element in s;
16+
3. dp[i+1][j] # * matches more than one in s.
17+
"""
18+
if not s:
19+
if p.count('*') != len(p):
20+
return False
21+
return True
22+
23+
# Optimized for the big data.
24+
if len(p) - p.count('*') > len(s):
25+
return False
26+
27+
# Initinal process
28+
dp = [[False for col in range(len(s) + 1)] for row in range(len(p) + 1)]
29+
dp[0][0] = True # isMatch("", "") = True
30+
for i in range(len(p)):
31+
dp[i + 1][0] = dp[i][0] and p[i] == '*'
32+
33+
for i in range(len(p)):
34+
for j in range(len(s)):
35+
if p[i] == "*":
36+
dp[i + 1][j + 1] = dp[i][j + 1] or dp[i][j] or dp[i + 1][j]
37+
else:
38+
dp[i + 1][j + 1] = dp[i][j] and (p[i] == s[j] or p[i] == "?")
39+
40+
return dp[len(p)][len(s)]
41+
42+
"""
43+
"aa"
44+
"a"
45+
"aa"
46+
"aa"
47+
"aaa"
48+
"aa"
49+
"aa"
50+
"*"
51+
"aa"
52+
"a*"
53+
"ab"
54+
"?*"
55+
"aab"
56+
"c*a*b"
57+
"""

DynamicProgramming/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888

8989
# 例子:更好的理解
9090

91+
## [44. Wildcard Matching](https://leetcode.com/problems/wildcard-matching/)
92+
9193
## [264 Ugly Number II](https://leetcode.com/problems/ugly-number-ii/)
9294

9395
> 把只含有因子 2、3和5的数称作丑数(Ugly Number),求按照从小到大的顺序第 1500 个丑数。例如6、8都是丑数,但14不是。习惯上把1当作是第一个丑数。

Greedy/45_JumpGameII.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* @Author: [email protected]
3+
* @Last Modified time: 2016-07-26 09:32:27
4+
*/
5+
6+
class Solution {
7+
public:
8+
/*
9+
When you can reach position i, find the next longest distance you can reach.
10+
11+
Once we can reach position i, we can find the next longest distance by iterate all
12+
the position before position i.
13+
14+
Of course, you can think it as a BFS problem.
15+
Where nodes in level i are all the nodes that can be reached in i-1th jump.
16+
For more explnation, goto:
17+
https://discuss.leetcode.com/topic/3191/o-n-bfs-solution
18+
*/
19+
int jump(vector<int>& nums) {
20+
if(nums.size() == 1) return 0;
21+
int step = 1, longest = nums[0];
22+
int index = 1;
23+
while(longest < nums.size()-1){
24+
int max_instance = 0;
25+
while(index <= longest){
26+
if(index + nums[index] > max_instance){
27+
max_instance = index + nums[index];
28+
}
29+
index += 1;
30+
}
31+
step += 1;
32+
longest = max_instance;
33+
}
34+
return step;
35+
}
36+
};
37+
38+
/*
39+
[0]
40+
[2,5,0,3]
41+
[2,3,1,1,4]
42+
[3,1,8,1,1,1,1,1,5]
43+
*/

Week06/45.py renamed to Greedy/45_JumpGameII.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44

55
class Solution(object):
66
def jump(self, nums):
7-
"""
8-
:type nums: List[int]
9-
:rtype: int
10-
"""
7+
""" When you can reach position i, find the next longest distance you can reach.
8+
9+
Once we can reach position i, we can find the next longest distance by iterate all
10+
the position before position i.
1111
12+
Of course, you can think it as a BFS problem.
13+
Where nodes in level i are all the nodes that can be reached in i-1th jump.
14+
For more explnation, goto:
15+
https://discuss.leetcode.com/topic/3191/o-n-bfs-solution
16+
"""
1217
if len(nums) == 1:
1318
return 0
1419

@@ -26,3 +31,10 @@ def jump(self, nums):
2631
step += 1
2732

2833
return step
34+
35+
"""
36+
[0]
37+
[2,5,0,3]
38+
[2,3,1,1,4]
39+
[3,1,8,1,1,1,1,1,5]
40+
"""

Math/43_MultiplyStrings.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @Author: [email protected]
3+
* @Last Modified time: 2016-07-25 20:13:40
4+
*/
5+
6+
class Solution {
7+
public:
8+
/*
9+
Simulation the manual way we do multiplication.
10+
11+
Start from right to left, perform multiplication on every pair of digits.
12+
And add them together.
13+
14+
There is a good graph explanation. Refer to:
15+
https://discuss.leetcode.com/topic/30508/easiest-java-solution-with-graph-explanation
16+
*/
17+
string multiply(string num1, string num2) {
18+
int m=num1.size(), n=num2.size();
19+
vector<int> pos(m+n, 0);
20+
for(int i=m-1; i>=0; i--){
21+
for(int j=n-1; j>=0; j--){
22+
int multi_sum = (num1[i] - '0') * (num2[j] - '0');
23+
int pos_sum = pos[i+j+1] + multi_sum;
24+
25+
// Update pos[i+j] and pos[i+j+1]
26+
pos[i+j] += pos_sum / 10;
27+
pos[i+j+1] = pos_sum % 10;
28+
}
29+
}
30+
string product = "";
31+
int i=0;
32+
for(; i<m+n && pos[i]==0; i++){};
33+
for(; i<m+n; i++){
34+
product += to_string(pos[i]);
35+
}
36+
return product == "" ? "0" : product;
37+
}
38+
};
39+
40+
/*
41+
"0"
42+
"1"
43+
"123"
44+
"123"
45+
"12121212121212125"
46+
"121232323499999252"
47+
*/

Math/43_MultiplyStrings.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
class Solution(object):
6+
def multiply(self, num1, num2):
7+
""" Simulation the manual way we do multiplication.
8+
9+
Start from right to left, perform multiplication on every pair of digits.
10+
And add them together.
11+
12+
There is a good graph explanation. Refer to:
13+
https://discuss.leetcode.com/topic/30508/easiest-java-solution-with-graph-explanation
14+
"""
15+
m, n = len(num1), len(num2)
16+
pos = [0] * (m + n)
17+
for i in range(m - 1, -1, -1):
18+
for j in range(n - 1, -1, -1):
19+
multi = int(num1[i]) * int(num2[j])
20+
pos_sum = pos[i + j + 1] + multi
21+
22+
# Update pos[i+j], pos[i+j+1]
23+
pos[i + j] += pos_sum / 10
24+
pos[i + j + 1] = pos_sum % 10
25+
26+
first_not_0 = 0
27+
while first_not_0 < m + n and pos[first_not_0] == 0:
28+
first_not_0 += 1
29+
30+
return "".join(map(str, pos[first_not_0:] or [0]))
31+
32+
"""
33+
"0"
34+
"1"
35+
"123"
36+
"123"
37+
"12121212121212125"
38+
"121232323499999252"
39+
"""

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@
178178

179179
* 010. [Regular Expression Matching](DynamicProgramming/10_RegularExpressionMatching.py)
180180
* 032. [Longest Valid Parentheses](DynamicProgramming/32_LongestValidParentheses.py)
181+
* 044. [Wildcard Matching](DynamicProgramming/44_WildcardMatching.py)
181182
* 053. [Maximum Subarray](DynamicProgramming/53_MaximumSubarray.py)
182183
* 062. [Unique Paths](DynamicProgramming/62_UniquePaths.py)
183184
* 063. [Unique Paths II](DynamicProgramming/63_UniquePathsII.py)
@@ -212,6 +213,7 @@
212213

213214
# [Greedy](Greedy/)
214215

216+
* 045. [Jump Game II](Greedy/45_JumpGameII.py)
215217
* 055. [Jump Game](Greedy/55_JumpGame.py)
216218
* 122. [Best Time to Buy and Sell Stock II](Greedy/122_BestTimeToBuyAndSellStockII.py)
217219
* 134. [Gas Station](Greedy/134_GasStation.py)
@@ -242,6 +244,7 @@
242244
* 016. [3Sum Closest](TwoPointers/16_3SumClosest.py)
243245
* 018. [4Sum](TwoPointers/18_4Sum.py)
244246
* 019. [Remove Nth Node From End of List](TwoPointers/19_RemoveNthNodeFromEndOfList.py)
247+
* 042. [Trapping Rain Water](TwoPointers/42_TrappingRainWater.py)
245248
* 075. [Sort Colors](TwoPointers/75_SortColors.py)
246249
* 080. [Remove Duplicates from Sorted Array II](TwoPointers/80_RemoveDuplicatesArrayII.py)
247250
* 088. [Merge Sorted Array](TwoPointers/88_MergeSortedArray.py)
@@ -261,6 +264,7 @@
261264
* 012. [Integer to Roman](Math/12_IntegertoRoman.py)
262265
* 013. [Roman to Integer](Math/13_RomantoInteger.py)
263266
* 048. [Rotate Image](Math/48_RotateImage.py)
267+
* 043. [Multiply Strings](Math/43_MultiplyStrings.py)
264268
* 050. [Pow(x, n)](Math/50_Pow.py)
265269
* 060. [Permutation Sequence](Math/60_PermutationSequence.py)
266270
* 066. [Plus One](Math/66_PlusOne.py)

TwoPointers/42_TrappingRainWater.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* @Author: [email protected]
3+
* @Last Modified time: 2016-07-25 18:31:59
4+
*/
5+
6+
class Solution {
7+
public:
8+
/*
9+
Search from left to right and maintain a max height of left and right separately,
10+
which is like a one-side wall of partial container.
11+
Fix the higher one and flow water from the lower part.
12+
For example, if current height of left is lower,
13+
we fill water in the left bin.
14+
Until left meets right, we filled the whole container.
15+
16+
Brilliant solution proposed by @mcrystal
17+
Refer to: https://discuss.leetcode.com/topic/5125/sharing-my-simple-c-code-o-n-time-o-1-space
18+
*/
19+
int trap(vector<int>& height) {
20+
int max_left=0, max_right = 0;
21+
int left = 0, right = height.size()-1;
22+
int ans = 0;
23+
while(left <= right){
24+
if(height[left] <= height[right]){
25+
if(height[left] >= max_left){
26+
max_left = height[left];
27+
}
28+
else{
29+
ans += max_left - height[left];
30+
}
31+
left ++;
32+
}
33+
else{
34+
if(height[right] >= max_right){
35+
max_right = height[right];
36+
}
37+
else{
38+
ans += max_right - height[right];
39+
}
40+
right --;
41+
}
42+
}
43+
return ans;
44+
}
45+
};
46+
47+
/*
48+
[]
49+
[3,0,0,3]
50+
[1,1,1,2,2,1,1]
51+
[0,1,0,2,1,0,1,3,2,1,2,1]
52+
*/

0 commit comments

Comments
 (0)