LeetCode.1706.球会落何处
难度:medium
?
?思路:模拟
class Solution {
public int[] findBall(int[][] grid) {
int len = grid.length;
int width = grid[0].length;
int[] ans = new int[width];
// 从i出发
for (int i = 0; i < width; i++) {
// 当前纵坐标为col
int col = i;
for (int row = 0; row < len; row++) {
// 向右
if (grid[row][col] == 1) {
col++;
if (col == width || grid[row][col] == -1) {
col = -1;
break;
}
// else if (grid[row][col] == 1) {
// col++;
// }
}
// 向左
if (grid[row][col] == -1) {
col--;
if (col < 0 || grid[row][col] == 1) {
col = -1;
break;
}
// else if(grid[row][col] == -1) {
// col--;
// }
}
}
ans[i] = col;
}
return ans;
}
}
复杂度分析:
- 时间复杂度:O(m * n),m行n列
- 空间复杂度: O(1)?
|