Skip to content

Commit 209015a

Browse files
committed
[Function add]
1. Add leetcode solutions.
1 parent 1e7d634 commit 209015a

4 files changed

+237
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,12 @@
469469

470470
[300. Longest Increasing Subsequence](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/300.%20Longest%20Increasing%20Subsequence.md)
471471

472+
[301. Remove Invalid Parentheses](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/301.%20Remove%20Invalid%20Parentheses.md)
473+
474+
[303. Range Sum Query - Immutable](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/303.%20Range%20Sum%20Query%20-%20Immutable.md)
475+
476+
[304. Range Sum Query 2D - Immutable](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/304.%20Range%20Sum%20Query%202D%20-%20Immutable.md)
477+
472478

473479
## Algorithm(4th_Edition)
474480
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
## 301. Remove Invalid Parentheses
2+
3+
### Question
4+
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.
5+
6+
Note: The input string may contain letters other than the parentheses ( and ).
7+
8+
```
9+
Example 1:
10+
11+
Input: "()())()"
12+
Output: ["()()()", "(())()"]
13+
14+
Example 2:
15+
16+
Input: "(a)())()"
17+
Output: ["(a)()()", "(a())()"]
18+
19+
Example 3:
20+
21+
Input: ")("
22+
Output: [""]
23+
```
24+
25+
### Thinking:
26+
* This question can use dfs to solve, and this question can be divided into 2 parts:
27+
1. Find the minimun '(' number and ')' number to delete.
28+
2. dfs: remove ')' first and then remove '('.
29+
```Java
30+
class Solution {
31+
public List<String> removeInvalidParentheses(String s) {
32+
int l = 0, r = 0;
33+
char[] arr = s.toCharArray();
34+
for(char c : arr){ // Check the minimum ( and ) to delete.
35+
if(c != '(' && c != ')') continue;
36+
if(c == '(') l++;
37+
else{
38+
if(l <= 0) r++;
39+
else l--;
40+
}
41+
}
42+
List<String> result = new LinkedList<>();
43+
dfs(s, l, r, 0, result);
44+
if(result.size() == 0) result.add("");
45+
return result;
46+
}
47+
private void dfs(String s, int l, int r, int index, List<String> result){
48+
if(l == 0 && r == 0 && isValid(s)) result.add(s); //if no ( or ) to delete and current string is valid, we can add this string to result list.
49+
char[] arr = s.toCharArray();
50+
for(int i = index; i < s.length(); i++){
51+
// if we have continuous ( or ), we just need to remove this first one so we won't get any duplicate result.
52+
if((arr[i] != '(' && arr[i] != ')') || (i > 0 && arr[i - 1] == arr[i])) continue;
53+
// We first delete ) for pruning
54+
if(r > 0 && arr[i] == ')'){
55+
// if we remove right, we need to decrease r and renew the current index.
56+
dfs(s.substring(0, i) + s.substring(i + 1), l, r - 1, i, result);
57+
}else if(l > 0 && arr[i] == '('){
58+
// if we remove right, we need to decrease l and renew the current index.
59+
dfs(s.substring(0, i) + s.substring(i + 1), l - 1, r, i, result);
60+
}
61+
}
62+
}
63+
private boolean isValid(String s){
64+
int count = 0;
65+
char[] arr = s.toCharArray();
66+
for(char c : arr){
67+
if(c != '(' && c != ')') continue;
68+
if(c == '(') count++;
69+
else{
70+
if(count > 0) count--;
71+
else return false;
72+
}
73+
}
74+
return true;
75+
}
76+
}
77+
```
78+
79+
### Reference
80+
1. [花花酱 LeetCode 301. Remove Invalid Parentheses - 刷题找工作 EP139](https://www.youtube.com/watch?v=2k_rS_u6EBk)

leetcode/303. Range Sum Query - Immutable.md

+89
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
## 303. Range Sum Query - Immutable
2+
3+
### Question:
4+
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
5+
6+
```
7+
Example:
8+
9+
Given nums = [-2, 0, 3, -5, 2, -1]
10+
11+
sumRange(0, 2) -> 1
12+
sumRange(2, 5) -> -1
13+
sumRange(0, 5) -> -3
14+
```
15+
16+
Note:
17+
18+
1. You may assume that the array does not change.
19+
2. There are many calls to sumRange function.
20+
221
### Thinking:
322
* Method 1:
423
* 通过dp,存储从开头加至当前的和。
@@ -17,6 +36,76 @@ class NumArray {
1736
return sum[j+1] - sum[i];
1837
}
1938
}
39+
/**
40+
* Your NumArray object will be instantiated and called as such:
41+
* NumArray obj = new NumArray(nums);
42+
* int param_1 = obj.sumRange(i,j);
43+
*/
44+
```
45+
46+
### Second time
47+
1. Use a 2-D array to save sum from i to j. MLE
48+
```Java
49+
class NumArray {
50+
private int[][]dp;
51+
public NumArray(int[] nums) {
52+
int len = nums.length;
53+
dp = new int[len][len];
54+
for(int i = 0; i < len; i++){
55+
for(int j = i; j < len; j++){
56+
dp[i][j] = nums[j] + (j > 0 ? dp[i][j - 1]: 0);
57+
}
58+
}
59+
}
60+
public int sumRange(int i, int j) {
61+
return dp[i][j];
62+
}
63+
}
64+
/**
65+
* Your NumArray object will be instantiated and called as such:
66+
* NumArray obj = new NumArray(nums);
67+
* int param_1 = obj.sumRange(i,j);
68+
*/
69+
```
70+
71+
2. Use for loop to calculate the result every time.
72+
```Java
73+
class NumArray {
74+
private int[] nums;
75+
public NumArray(int[] nums) {
76+
this.nums = nums;
77+
}
78+
public int sumRange(int i, int j) {
79+
int sum = 0;
80+
for(int s = i; s <= j; s++){
81+
sum += nums[s];
82+
}
83+
return sum;
84+
}
85+
}
86+
/**
87+
* Your NumArray object will be instantiated and called as such:
88+
* NumArray obj = new NumArray(nums);
89+
* int param_1 = obj.sumRange(i,j);
90+
*/
91+
```
92+
93+
3. We only need one dimension array to save the sum of the array up to current position, when we need to calculate sumRange(i, j), we can use sum[j] - sum[i - 1] to get the result;
94+
```Java
95+
class NumArray {
96+
private int[] sum;
97+
public NumArray(int[] nums) {
98+
this.sum = new int[nums.length];
99+
int count = 0;
100+
for(int i = 0; i < nums.length; i++){
101+
count += nums[i];
102+
sum[i] = count;
103+
}
104+
}
105+
public int sumRange(int i, int j) {
106+
return sum[j] - (i > 0 ? sum[i - 1] : 0);
107+
}
108+
}
20109
/**
21110
* Your NumArray object will be instantiated and called as such:
22111
* NumArray obj = new NumArray(nums);

leetcode/304. Range Sum Query 2D - Immutable.md

+62
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,31 @@
11
## 304. Range Sum Query 2D - Immutable
2+
3+
### Question:
4+
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).
5+
6+
```
7+
Example:
8+
9+
Given matrix = [
10+
[3, 0, 1, 4, 2],
11+
[5, 6, 3, 2, 1],
12+
[1, 2, 0, 1, 5],
13+
[4, 1, 0, 1, 7],
14+
[1, 0, 3, 0, 5]
15+
]
16+
17+
sumRegion(2, 1, 4, 3) -> 8
18+
sumRegion(1, 1, 2, 2) -> 11
19+
sumRegion(1, 2, 2, 4) -> 12
20+
```
21+
22+
Note:
23+
1. You may assume that the matrix does not change.
24+
2. There are many calls to sumRegion function.
25+
3. You may assume that row1 ≤ row2 and col1 ≤ col2.
26+
27+
28+
229
### Thinking:
330
* Method 1:
431
* 参考[303. Range Sum Query - Immutable](https://github.com/Seanforfun/Algorithm/blob/master/leetcode/303.%20Range%20Sum%20Query%20-%20Immutable.md)
@@ -68,6 +95,41 @@ class NumMatrix {
6895
return result;
6996
}
7097
}
98+
/**
99+
* Your NumMatrix object will be instantiated and called as such:
100+
* NumMatrix obj = new NumMatrix(matrix);
101+
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
102+
*/
103+
```
104+
105+
### Second time
106+
1. Use a 2-D array to save the rectangle sum from (0, 0) to current point.
107+
```Java
108+
class NumMatrix {
109+
private int[][] dp;
110+
public NumMatrix(int[][] matrix) {
111+
if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
112+
dp = new int[0][0];
113+
return;
114+
}
115+
int height = matrix.length, width = matrix[0].length;
116+
dp = new int[height][width];
117+
for(int i = 0; i < height; i++){
118+
for(int j = 0; j < width; j++){
119+
int cur = 0;
120+
for(int p = 0; p <= i; p++){
121+
cur += matrix[p][j];
122+
}
123+
dp[i][j] = (j > 0 ? dp[i][j - 1]: 0) + cur;
124+
}
125+
}
126+
}
127+
public int sumRegion(int row1, int col1, int row2, int col2) {
128+
return dp[row2][col2] - (col1 > 0 ? dp[row2][col1 - 1]: 0)
129+
- (row1 > 0 ? dp[row1 - 1][col2]: 0)
130+
+ (col1 > 0 && row1 > 0 ? dp[row1 - 1][col1 - 1]: 0);
131+
}
132+
}
71133
/**
72134
* Your NumMatrix object will be instantiated and called as such:
73135
* NumMatrix obj = new NumMatrix(matrix);

0 commit comments

Comments
 (0)