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 b1e3d8c

Browse files
committed
feat: add solutions to lc problem: No.1351. Count Negative Numbers in a Sorted Matrix
1 parent 913a54b commit b1e3d8c

File tree

6 files changed

+187
-3
lines changed

6 files changed

+187
-3
lines changed

‎solution/1300-1399/1351.Count Negative Numbers in a Sorted Matrix/README.md‎

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,93 @@
5858

5959
<p> </p>
6060

61-
6261
## 解法
6362

6463
<!-- 这里可写通用的实现逻辑 -->
6564

65+
从右上角开始遍历。当遇到负数时,说明这一列从当前行往下的所有数(共 `m - i` 个数)均为负数,`cnt += (m - i)`,然后 j 往左移动一个位置。否则 i 往下移动一个位置。
66+
67+
最后返回 cnt 值即可。
68+
6669
<!-- tabs:start -->
6770

6871
### **Python3**
6972

7073
<!-- 这里可写当前语言的特殊实现逻辑 -->
7174

7275
```python
73-
76+
class Solution:
77+
def countNegatives(self, grid: List[List[int]]) -> int:
78+
m, n, cnt = len(grid), len(grid[0]), 0
79+
i, j = 0, n - 1
80+
while i < m and j >= 0:
81+
if grid[i][j] < 0:
82+
cnt += (m - i)
83+
j -= 1
84+
else:
85+
i += 1
86+
return cnt
7487
```
7588

7689
### **Java**
7790

7891
<!-- 这里可写当前语言的特殊实现逻辑 -->
7992

8093
```java
94+
class Solution {
95+
public int countNegatives(int[][] grid) {
96+
int m = grid.length, n = grid[0].length;
97+
int cnt = 0;
98+
for (int i = 0, j = n - 1; j >= 0 && i < m;) {
99+
if (grid[i][j] < 0) {
100+
cnt += (m - i);
101+
--j;
102+
} else {
103+
++i;
104+
}
105+
}
106+
return cnt;
107+
}
108+
}
109+
```
110+
111+
### **C++**
112+
113+
```cpp
114+
class Solution {
115+
public:
116+
int countNegatives(vector<vector<int>>& grid) {
117+
int m = grid.size(), n = grid[0].size();
118+
int i = 0, j = n - 1, cnt = 0;
119+
while (i < m && j >= 0) {
120+
if (grid[i][j] < 0) {
121+
cnt += (m - i);
122+
--j;
123+
} else {
124+
++i;
125+
}
126+
}
127+
return cnt;
128+
}
129+
};
130+
```
81131
132+
### **Go**
133+
134+
```go
135+
func countNegatives(grid [][]int) int {
136+
m, n := len(grid), len(grid[0])
137+
i, j, cnt := 0, n-1, 0
138+
for i < m && j >= 0 {
139+
if grid[i][j] < 0 {
140+
cnt += (m - i)
141+
j--
142+
} else {
143+
i++
144+
}
145+
}
146+
return cnt
147+
}
82148
```
83149

84150
### **...**

‎solution/1300-1399/1351.Count Negative Numbers in a Sorted Matrix/README_EN.md‎

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,76 @@
5656
### **Python3**
5757

5858
```python
59-
59+
class Solution:
60+
def countNegatives(self, grid: List[List[int]]) -> int:
61+
m, n, cnt = len(grid), len(grid[0]), 0
62+
i, j = 0, n - 1
63+
while i < m and j >= 0:
64+
if grid[i][j] < 0:
65+
cnt += (m - i)
66+
j -= 1
67+
else:
68+
i += 1
69+
return cnt
6070
```
6171

6272
### **Java**
6373

6474
```java
75+
class Solution {
76+
public int countNegatives(int[][] grid) {
77+
int m = grid.length, n = grid[0].length;
78+
int cnt = 0;
79+
for (int i = 0, j = n - 1; j >= 0 && i < m;) {
80+
if (grid[i][j] < 0) {
81+
cnt += (m - i);
82+
--j;
83+
} else {
84+
++i;
85+
}
86+
}
87+
return cnt;
88+
}
89+
}
90+
```
91+
92+
### **C++**
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
int countNegatives(vector<vector<int>>& grid) {
98+
int m = grid.size(), n = grid[0].size();
99+
int i = 0, j = n - 1, cnt = 0;
100+
while (i < m && j >= 0) {
101+
if (grid[i][j] < 0) {
102+
cnt += (m - i);
103+
--j;
104+
} else {
105+
++i;
106+
}
107+
}
108+
return cnt;
109+
}
110+
};
111+
```
65112
113+
### **Go**
114+
115+
```go
116+
func countNegatives(grid [][]int) int {
117+
m, n := len(grid), len(grid[0])
118+
i, j, cnt := 0, n-1, 0
119+
for i < m && j >= 0 {
120+
if grid[i][j] < 0 {
121+
cnt += (m - i)
122+
j--
123+
} else {
124+
i++
125+
}
126+
}
127+
return cnt
128+
}
66129
```
67130

68131
### **...**
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int countNegatives(vector<vector<int>>& grid) {
4+
int m = grid.size(), n = grid[0].size();
5+
int i = 0, j = n - 1, cnt = 0;
6+
while (i < m && j >= 0) {
7+
if (grid[i][j] < 0) {
8+
cnt += (m - i);
9+
--j;
10+
} else {
11+
++i;
12+
}
13+
}
14+
return cnt;
15+
}
16+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func countNegatives(grid [][]int) int {
2+
m, n := len(grid), len(grid[0])
3+
i, j, cnt := 0, n-1, 0
4+
for i < m && j >= 0 {
5+
if grid[i][j] < 0 {
6+
cnt += (m - i)
7+
j--
8+
} else {
9+
i++
10+
}
11+
}
12+
return cnt
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int countNegatives(int[][] grid) {
3+
int m = grid.length, n = grid[0].length;
4+
int cnt = 0;
5+
for (int i = 0, j = n - 1; j >= 0 && i < m;) {
6+
if (grid[i][j] < 0) {
7+
cnt += (m - i);
8+
--j;
9+
} else {
10+
++i;
11+
}
12+
}
13+
return cnt;
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def countNegatives(self, grid: List[List[int]]) -> int:
3+
m, n, cnt = len(grid), len(grid[0]), 0
4+
i, j = 0, n - 1
5+
while i < m and j >= 0:
6+
if grid[i][j] < 0:
7+
cnt += (m - i)
8+
j -= 1
9+
else:
10+
i += 1
11+
return cnt

0 commit comments

Comments
(0)

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