Skip to content

Commit

Permalink
Create 1706.Where-Will-the-Ball-Fall.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Dec 28, 2020
1 parent 8fb6140 commit 3aeafb0
Showing 1 changed file with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Solution {
public:
vector<int> findBall(vector<vector<int>>& grid)
{
int m = grid.size();
int n = grid[0].size();

vector<int>ret(n,-1);
for (int j=0; j<n; j++)
{
int x = 0, y = j;
while (x < m)
{
if (grid[x][y]==1 && y==n-1)
break;
else if (grid[x][y]==-1 && y==0)
break;
else if (grid[x][y]==1 && grid[x][y+1]==-1)
break;
else if (grid[x][y]==-1 && grid[x][y-1]==1)
break;
else if (grid[x][y]==1)
{
x++;
y++;
}
else
{
x++;
y--;
}
}
if (x==m)
ret[j] = y;
}
return ret;
}
};

0 comments on commit 3aeafb0

Please sign in to comment.