Skip to content

Commit 7f4686c

Browse files
committed
add 264, 304
1 parent ca88a3d commit 7f4686c

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int nthUglyNumber(int n) {
4+
vector<int> res(1, 1);
5+
int i2 = 0, i3 = 0, i5 = 0;
6+
while (res.size() < n) {
7+
int m2 = res[i2] * 2;
8+
int m3 = res[i3] * 3;
9+
int m5 = res[i5] * 5;
10+
int mn = min(m2, min(m3, m5));
11+
if (mn == m2) ++i2;
12+
if (mn == m3) ++i3;
13+
if (mn == m5) ++i5;
14+
res.push_back(mn);
15+
}
16+
return res.back();
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class NumMatrix {
2+
public:
3+
vector<vector<int>> dp;
4+
int row;
5+
int col;
6+
NumMatrix(vector<vector<int>> matrix) {
7+
row = matrix.size();
8+
col = row>0?matrix[0].size():0;
9+
dp = vector<vector<int>>(row+1, vector<int>(col+1,0));
10+
11+
//
12+
for(int i=1;i<row+1;i++)
13+
for(int j=1;j<col+1;j++)
14+
dp[i][j] = matrix[i-1][j-1] + dp[i][j-1] + dp[i-1][j] - dp[i-1][j-1];
15+
}
16+
17+
int sumRegion(int row1, int col1, int row2, int col2) {
18+
return dp[row2+1][col2+1] - dp[row2+1][col1] - dp[row1][col2+1] + dp[row1][col1];
19+
}
20+
};
21+
22+
/**
23+
* Your NumMatrix object will be instantiated and called as such:
24+
* NumMatrix obj = new NumMatrix(matrix);
25+
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
26+
*/

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,8 @@
425425

426426
[263.Ugly-Number](Algorithms/263.Ugly-Number/solution.cpp)
427427

428+
[264.Ugly-Number-II](Algorithms/264.Ugly-Number-II/solution.cpp)
429+
428430
[273.Integer-to-English-Words](Algorithms/273.Integer-to-English-Words/solution.cpp)
429431

430432
[278.First-Bad-Version](Algorithms/278.First-Bad-Version/solution.cpp)
@@ -443,6 +445,8 @@
443445

444446
[303.Range-Sum-Query---Immutable](Algorithms/303.Range-Sum-Query---Immutable/solution.cpp)
445447

448+
[304.Range-Sum-Query-2D---Immutable](Algorithms/304.Range-Sum-Query-2D---Immutable/solution.cpp)
449+
446450
[309.Best-Time-to-Buy-and-Sell-Stock-with-Cooldown](Algorithms/309.Best-Time-to-Buy-and-Sell-Stock-with-Cooldown/solution.cpp)
447451

448452
[318.Maximum-Product-of-Word-Lengths](Algorithms/318.Maximum-Product-of-Word-Lengths/solution.cpp)

0 commit comments

Comments
 (0)