365天挑战LeetCode1000题——Day 093 球会落何处 循环轮转矩阵

1706. 球会落何处

在这里插入图片描述

代码实现(模拟)

class Solution {
public:
    vector<int> findBall(vector<vector<int>>& grid) {
        int rows = grid.size();
        int cols = grid[0].size();
        vector<int> ans;
        for (int i = 0; i < grid[0].size(); i++) {
            int pos = i;
            for (int j = 0; j < grid.size(); j++) {
                if (grid[j][pos] == 1) {
                    // right is 1, go down
                    if (pos == cols - 1 || grid[j][pos + 1] == -1) {
                        pos = -1;
                        break;
                    }
                    else {
                        pos += 1;
                    }
                }
                else if (grid[j][pos] = -1) {
                    // left is -1, go down, else stuck
                    if (pos == 0 || grid[j][pos - 1] == 1) {
                        pos = -1;
                        break;
                    }
                    else {
                        pos -= 1;
                    }
                }
            }
            ans.push_back(pos);
        }
        return ans;
    }
};

1914. 循环轮转矩阵

代码实现(模拟)

class Solution {
public:
    vector<vector<int>> rotateGrid(vector<vector<int>>& grid, int k) {
        const int rows = grid.size();
        const int cols = grid[0].size();
        const int times = min(rows / 2, cols / 2);
        deque<int> cycle;
        for (int t = 0; t < times; t++) {
            int i = t, j = t;
            int r = rows - 2 * t;
            int c = cols - 2 * t;
            // cout << r << " " << c << endl;
            cycle.clear();
            // read all the element
            for (int m = t; m < t + r; m++) {
                cycle.push_back(grid[m][t]);
                // cout << m << " " << t << endl;
            }
            for (int n = t + 1; n < t + c; n++) {
                cycle.push_back(grid[r + t - 1][n]);
                // cout << r + t - 1 << " " << n << endl;
            }
            for (int m = r + t - 2; m >= t; m--) {
                // cout << m << " " << t + c - 1 << endl;
                cycle.push_back(grid[m][t + c - 1]);
            }
            for (int n = t + c - 2; n > t; n--) {
                cycle.push_back(grid[t][n]);
                // cout << t << " " << n << endl;
            }
            int curK = k % (2 * (r + c) - 4);
            for (int i = 0; i < curK; i++) {
                int tmp = cycle.back();
                // cout << tmp << endl;
                cycle.pop_back();
                cycle.push_front(tmp);
            }
            for (int m = t; m < t + r; m++) {
                grid[m][t] = cycle.front();
                cycle.pop_front();
            }
            for (int n = t + 1; n < t + c; n++) {
                grid[r + t - 1][n] = cycle.front();
                cycle.pop_front();
            }
            for (int m = r + t - 2; m >= t; m--) {
                grid[m][t + c - 1] = cycle.front();
                cycle.pop_front();
            }
            for (int n = t + c - 2; n > t; n--) {
                grid[t][n] = cycle.front();
                cycle.pop_front();
            }
        }
        return grid;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值