File tree 3 files changed +48
-0
lines changed
304.Range-Sum-Query-2D---Immutable
3 files changed +48
-0
lines changed Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ */
Original file line number Diff line number Diff line change 425
425
426
426
[ 263.Ugly-Number] ( Algorithms/263.Ugly-Number/solution.cpp )
427
427
428
+ [ 264.Ugly-Number-II] ( Algorithms/264.Ugly-Number-II/solution.cpp )
429
+
428
430
[ 273.Integer-to-English-Words] ( Algorithms/273.Integer-to-English-Words/solution.cpp )
429
431
430
432
[ 278.First-Bad-Version] ( Algorithms/278.First-Bad-Version/solution.cpp )
443
445
444
446
[ 303.Range-Sum-Query---Immutable] ( Algorithms/303.Range-Sum-Query---Immutable/solution.cpp )
445
447
448
+ [ 304.Range-Sum-Query-2D---Immutable] ( Algorithms/304.Range-Sum-Query-2D---Immutable/solution.cpp )
449
+
446
450
[ 309.Best-Time-to-Buy-and-Sell-Stock-with-Cooldown] ( Algorithms/309.Best-Time-to-Buy-and-Sell-Stock-with-Cooldown/solution.cpp )
447
451
448
452
[ 318.Maximum-Product-of-Word-Lengths] ( Algorithms/318.Maximum-Product-of-Word-Lengths/solution.cpp )
You can’t perform that action at this time.
0 commit comments