zoukankan      html  css  js  c++  java
  • LeetCode59. 螺旋矩阵 II


    这题和第54题类似,都是套一个搜索的模板。
    用dx和dy表示方向,方向的顺序是先向右,再向下,再向左,再向上,再向右。。。
    如果“撞墙”了就需要改变到下一个方向。“撞墙”的判定就是(newX, newY)越界或者已经被访问过。
    “撞墙”就需要改变方向,即更新(newX, newY)。

    class Solution {
    public:
        vector<vector<int>> generateMatrix(int n) {
            vector<vector<int>> res(n, vector<int>(n));
            vector<vector<bool>> visited(n, vector<bool>(n));
            int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
            for(int x = 0, y = 0, direction = 0, cnt = 1; cnt <= n * n; ++cnt) {
                res[x][y] = cnt;
                visited[x][y] = true;
                int newX = x + dx[direction], newY = y + dy[direction];
                if(newX < 0 || newX >= n || newY < 0 || newY >= n || visited[newX][newY] == true) {
                    direction = (direction + 1) % 4;
                    newX = x + dx[direction], newY = y + dy[direction];
                }
                x = newX, y = newY;
            }
            return res;
        }
    };
    
  • 相关阅读:
    (三)数据预处理过程简介
    (二)数据挖掘需要哪些技术
    (一) 什么是数据挖掘?
    怎样提问呢?
    什么是自我意识
    高效能人士的七个习惯
    “U”型读书法
    CMDB学习之一
    层级选择器
    css选择器
  • 原文地址:https://www.cnblogs.com/linrj/p/13196999.html
Copyright © 2011-2022 走看看