885-螺旋矩阵 - III

在 R 行 C 列的矩阵上,我们从 (r0, c0) 面朝东面开始

这里,网格的西北角位于第一行第一列,网格的东南角位于最后一行最后一列。

现在,我们以顺时针按螺旋状行走,访问此网格中的每个位置。

每当我们移动到网格的边界之外时,我们会继续在网格之外行走(但稍后可能会返回到网格边界)。

最终,我们到过网格的所有 R * C 个空间。

按照访问顺序返回表示网格位置的坐标列表。

示例 1:

输入:R = 1, C = 4, r0 = 0, c0 = 0
输出:[[0,0],[0,1],[0,2],[0,3]]

示例 2:

输入:R = 5, C = 6, r0 = 1, c0 = 4
输出:[[1,4],[1,5],[2,5],[2,4],[2,3],[1,3],[0,3],[0,4],[0,5],[3,5],[3,4],[3,3],[3,2],[2,2],[1,2],[0,2],[4,5],[4,4],[4,3],[4,2],[4,1],[3,1],[2,1],[1,1],[0,1],[4,0],[3,0],[2,0],[1,0],[0,0]]

提示:

  • 1 <= R <= 100
  • 1 <= C <= 100
  • 0 <= r0 < R
  • 0 <= c0 < C

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/spiral-matrix-iii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    public int[][] spiralMatrixIII(int R, int C, int r0, int c0) {
        int[][] res = new int[R * C][2];

        int num = 0;
        int cnt = 1;
        boolean[][] visited = new boolean[R][C];
        while (num < R * C) {

            for(int j = c0 - cnt + 1; j <= c0 + cnt; j++) {
                if(r0 + 1 - cnt >= 0 && r0 + 1 - cnt < R && j >= 0 && j < C
                        && !visited[r0 + 1 - cnt][j]) {
                    res[num++] = new int[]{r0 - cnt + 1, j};
                    visited[r0 + 1 - cnt][j] = true;
                }
            }

            for(int i = r0 - cnt + 1; i <= r0 + cnt; i++) {
                if(i >= 0 && i < R && c0 + cnt >= 0 && c0 + cnt < C
                        && !visited[i][c0 + cnt]) {
                    res[num++] = new int[]{i, c0 + cnt};
                    visited[i][c0 + cnt] = true;
                }
            }

            for (int j = c0 + cnt; j >= c0 - cnt; j--) {
                if(r0 + cnt >= 0 && r0 + cnt < R && j >= 0 && j < C
                        && !visited[r0 + cnt][j]) {
                    res[num++] = new int[]{r0 + cnt, j};
                    visited[r0 + cnt][j] = true;
                }
            }

            for (int i = r0 + cnt; i >= r0 - cnt; i--) {
                if(i >= 0 && i < R && c0 - cnt >= 0 && c0 - cnt < C
                        && !visited[i][c0 - cnt]) {
                    res[num++] = new int[]{i, c0 - cnt};
                    visited[i][c0 - cnt] = true;
                }
            }

            cnt++;
        }
        return res;
    }

官方题解:

class Solution {
    public int[][] spiralMatrixIII(int R, int C, int r0, int c0) {
        int[] dr = new int[]{0, 1, 0, -1};
        int[] dc = new int[]{1, 0, -1, 0};

        int[][] ans = new int[R*C][2];
        int t = 0;

        ans[t++] = new int[]{r0, c0};
        if (R * C == 1) return ans;

        for (int k = 1; k < 2*(R+C); k += 2)
            for (int i = 0; i < 4; ++i) {  // i: direction index
                int dk = k + (i / 2);  // number of steps in this direction
                for (int j = 0; j < dk; ++j) {  // for each step in this direction...
                    // step in the i-th direction
                    r0 += dr[i];
                    c0 += dc[i];
                    if (0 <= r0 && r0 < R && 0 <= c0 && c0 < C) {
                        ans[t++] = new int[]{r0, c0};
                        if (t == R * C) return ans;
                    }
                }
            }

        throw null;
    }
}

作者:LeetCode
链接:https://leetcode-cn.com/problems/spiral-matrix-iii/solution/luo-xuan-ju-zhen-iii-by-leetcode/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

最新文章

  1. spider RPC过滤器
  2. 你用过这种奇葩的C#注释吗?如何看待
  3. 【leetcode】Maximal Rectangle (hard)★
  4. 轻量级应用开发之(04)UIScrollView-1
  5. Shell 函数 function [转]
  6. Beyond Compare 设置打开文件的默认编码
  7. 被误解的 MVC 和被神化的 MVVM
  8. dos批量替换当前目录后缀名
  9. RxAndroid中observable的基本使用和表单校验操作
  10. 关于WebApi 跨域问题的解决的方式
  11. js中==和===区别
  12. [bzoj4763]雪辉&amp;[bzoj4812][Ynoi2017]由乃打扑克
  13. L1,L2正则化代码
  14. dubbo源码之服务消费
  15. Linux替换字符串
  16. [LeetCode] 203. Remove Linked List Elements_Easy tag: Linked LIst
  17. css中display为none 和visibility为hidden的区别
  18. 科学计算三维可视化---TVTK管线与数据加载(可视化管线和图像管线了解)
  19. hdu 5308 (2015多校第二场第9题)脑洞模拟题,无语
  20. libevent库介绍--事件和数据缓冲

热门文章

  1. 使用Python批量更新服务器文件【新手必学】
  2. StackExchange.Redis 之 Set集合 类型示例
  3. Binder基本使用
  4. P3078 [USACO13MAR]Poker Hands S
  5. System.Text.Json 自定义Converter实现时间转换
  6. opencv —— HoughLines、HoughLinesP 霍夫线变换原理(标准霍夫线变换、多尺度霍夫线变换、累积概率霍夫线变换)及直线检测
  7. 离散对数及其拓展 大步小步算法 BSGS
  8. 基于90nm CMOS技术的功能齐全的64Mb DDR3 STT-MRAM
  9. 第3章 关系数据库标准语言SQL(重点) | 数据库知识点整理
  10. AOP配置步骤(XML)