0
点赞
收藏
分享

微信扫一扫

leetcode算法题--球会落何处

花明 2022-03-11 阅读 52

原题链接:https://leetcode-cn.com/problems/where-will-the-ball-fall/

class Solution {
public:
    vector<int> findBall(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid[0].size();
        vector<int> res(n, INT_MAX);
        for (int j = 0; j < n; ++j) {
            int y = j;
            for (auto row : grid) {
                int dir = row[y];
                y += dir;
                if (y < 0 || y >= n || row[y] != dir) {
                    res[j] = -1;
                    break;
                }
                res[j] = y;
            }
        }
        return res;
    }
};
举报

相关推荐

0 条评论