-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path1706.Where-Will-the-Ball-Fall.java
52 lines (45 loc) · 1.35 KB
/
1706.Where-Will-the-Ball-Fall.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// https://leetcode.com/problems/where-will-the-ball-fall/
// algorithms
// Medium (56.43%)
// Total Accepted: 5,023
// Total Submissions: 8,901
class Solution {
public int[] findBall(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
if (n == 1) {
return new int[]{-1};
}
int[] res = new int[n];
for (int i = 0; i < n; i++) {
int x = 0, y = i;
while (x < m) {
if (y == 0 && grid[x][y] == -1) {
y = -1;
break;
} else if (y == n - 1 && grid[x][y] == 1) {
y = -1;
break;
} else {
if (grid[x][y] == 1) {
if (grid[x][y + 1] == -1) {
y = -1;
break;
}
x++;
y++;
} else {
if (grid[x][y - 1] == 1) {
y = -1;
break;
}
x++;
y--;
}
}
}
res[i] = y;
}
return res;
}
}