Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit acdb137

Browse files
committed
add solution 1074,1476,1535,1552,1588
1 parent 83d5412 commit acdb137

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public int numSubmatrixSumTarget(int[][] matrix, int target) {
3+
int row = matrix.length, col = matrix[0].length;
4+
int[][] sum = new int[row][col];
5+
int ans = 0;
6+
for (int i = 0; i < row; i++) {
7+
for (int j = 0; j < col; j++) {
8+
if (i == 0 && j == 0) {
9+
sum[i][j] = matrix[i][j];
10+
} else if (i == 0) {
11+
sum[i][j] = matrix[i][j] + sum[i][j - 1];
12+
} else if (j == 0) {
13+
sum[i][j] = matrix[i][j] + sum[i - 1][j];
14+
} else {
15+
sum[i][j] = matrix[i][j] - sum[i - 1][j - 1] + sum[i - 1][j] + sum[i][j - 1];
16+
}
17+
for (int k = 0; k <= i; k++) {
18+
for (int l = 0; l <= j; l++) {
19+
int main = (k != 0 && l != 0) ? sum[k - 1][l - 1] : 0;
20+
int left = k != 0 ? sum[k - 1][j] : 0;
21+
int up = l != 0 ? sum[i][l - 1] : 0;
22+
if (sum[i][j] - left - up + main == target) {
23+
ans++;
24+
}
25+
}
26+
}
27+
}
28+
}
29+
return ans;
30+
}
31+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class SubrectangleQueries {
2+
3+
int[][] matrix;
4+
5+
public SubrectangleQueries(int[][] rectangle) {
6+
matrix = new int[rectangle.length][rectangle[0].length];
7+
for (int i = 0; i < rectangle.length; i++) {
8+
for (int j = 0; j < rectangle[0].length; j++) {
9+
matrix[i][j] = rectangle[i][j];
10+
}
11+
}
12+
}
13+
14+
public void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
15+
if (row1 > row2 || col1 > col2) {
16+
return;
17+
}
18+
for (int i = row1; i <= row2; i++) {
19+
for (int j = col1; j <= col2; j++) {
20+
matrix[i][j] = newValue;
21+
}
22+
}
23+
}
24+
25+
public int getValue(int row, int col) {
26+
return matrix[row][col];
27+
}
28+
}
29+
30+
/**
31+
* Your SubrectangleQueries object will be instantiated and called as such:
32+
* SubrectangleQueries obj = new SubrectangleQueries(rectangle);
33+
* obj.updateSubrectangle(row1,col1,row2,col2,newValue);
34+
* int param_2 = obj.getValue(row,col);
35+
*/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int getWinner(int[] arr, int k) {
3+
int time = 0, max = arr[0];
4+
for (int i = 1; i < arr.length; i++) {
5+
if (max > arr[i]) {
6+
time++;
7+
} else {
8+
time = 1;
9+
max = arr[i];
10+
}
11+
if (time >= k) {
12+
return max;
13+
}
14+
}
15+
return max;
16+
}
17+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public int maxDistance(int[] position, int m) {
3+
Arrays.sort(position);
4+
// 最小磁力的可能最小值
5+
int min = 1;
6+
// 最小磁力的可能最大值
7+
int max = (position[position.length - 1] - position[0]) / (m - 1);
8+
int ans = -1;
9+
while (min <= max) {
10+
int mid = (min + max) / 2;
11+
if (checkDistance(mid, position, m)) {
12+
ans = mid;
13+
min = mid + 1;
14+
} else {
15+
max = mid - 1;
16+
}
17+
}
18+
return ans;
19+
}
20+
21+
private boolean checkDistance(int mid, int[] position, int m) {
22+
int count = 1;
23+
int pre = position[0];
24+
for (int i = 1; i < position.length; i++) {
25+
if (position[i] - pre >= mid) {
26+
count++;
27+
if (count >= m) {
28+
return true;
29+
}
30+
pre = position[i];
31+
}
32+
}
33+
return false;
34+
}
35+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int sumOddLengthSubarrays(int[] arr) {
3+
int[] sum = new int[arr.length];
4+
for (int i = 0; i < arr.length; i++) {
5+
sum[i] = (i != 0 ? sum[i - 1] : 0) + arr[i];
6+
}
7+
int ans = 0;
8+
// sum[b] - sum[a] 为 (a,b] 的和
9+
for (int i = 0; i < arr.length; i++) {
10+
ans += arr[i];
11+
for (int j = i + 2; j < arr.length; j += 2) {
12+
// [i, j]
13+
ans += sum[j] - sum[i] + arr[i];
14+
}
15+
}
16+
return ans;
17+
}
18+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /