zoukankan      html  css  js  c++  java
  • Spiral Matrix I&&II

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

    For example,
    Given the following matrix:

    [
     [ 1, 2, 3 ],
     [ 4, 5, 6 ],
     [ 7, 8, 9 ]
    ]
    

    You should return [1,2,3,6,9,8,7,4,5].

    这道题挺简单的,基本上算是一次性写出来的,就是设立一个对应的标志数组,然后按照螺旋的规则来遍历。

    代码如下:

    class Solution {
    public:
        vector<int> spiralOrder(vector<vector<int>>& matrix) {
            vector<int> res;
            int height=matrix.size();
            if( height==0)
                return res;
            int width=matrix[0].size();
            vector<vector<int>> flag(height,vector<int>(width,1));//用来记录是否走过
            int m=0;
            int n=0;
            flag[0][0]=0;
            res.push_back(matrix[0][0]);
            int step=1;
            while(step!= height* width)
            {
                while(n+1<width&&flag[m][n+1])
                {
                    flag[m][n+1]=0;
                    res.push_back(matrix[m][n+1]);
                    n+=1;
                    step++;
                }
                while(m+1<height&&flag[m+1][n])
                {
                    flag[m+1][n]=0;
                    res.push_back(matrix[m+1][n]);
                    m+=1;
                    step++;
                }
                while(n-1>=0&&flag[m][n-1])
                {
                    flag[m][n-1]=0;
                    res.push_back(matrix[m][n-1]);
                    n-=1;
                    step++;
                }
                while(m-1>=0&&flag[m-1][n])
                {
                    flag[m-1][n]=0;
                    res.push_back(matrix[m-1][n]);
                    m-=1;
                    step++;
                }
            }
            return res;
        }
    };

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.

    For example,
    Given n = 3,

    You should return the following matrix:

    [
     [ 1, 2, 3 ],
     [ 8, 9, 4 ],
     [ 7, 6, 5 ]
    ]
    

     I写出来了的话,II就更简单了,在I上改一下就行了,代码如下:

    class Solution {
    public:
        vector<vector<int>> generateMatrix(int n) {
            vector<vector<int>> flag(n,vector<int>(n,0));//用来记录是否走过
            if(n==0)
                return flag;
            int height=0;
            int width=0;
            int step=1;
            flag[0][0]=1;
            while(step!= n*n)
            {
                while(width+1<n&&flag[height][width+1]==0)
                {
                    width+=1;
                    step++;
                    flag[height][width]=step;
                }
                while(height+1<n&&flag[height+1][width]==0)
                {
                    height+=1;
                    step++;
                    flag[height][width]=step;
                }
                while(width-1>=0&&flag[height][width-1]==0)
                {
                    width-=1;
                    step++;
                    flag[height][width]=step;
                }
                while(height-1>=0&&flag[height-1][width]==0)
                {
                    height-=1;
                    step++;
                    flag[height][width]=step;
                }
            }
            return flag;
            
        }
    };

      

  • 相关阅读:
    给大家介绍几个网站学习前端和服务器语言的网站吧,一定有合适你的
    centos用yum安装软件提示:另外一个程序锁定了 yum;等待它退出
    模仿小猎CMS首页功能展示的JS效果
    在centos下安装Node.js 开发环境搭建
    discuz在IIS,apache中分别怎么设置伪静态
    CentOS系统下各文件夹的作用
    centos上网设置
    php微信公众平台开发获取access_token,用CURL出现certificate verify failed错误的解决方法
    12.9 NIO
    12.8 Java 9改进的对象序列化
  • 原文地址:https://www.cnblogs.com/qiaozhoulin/p/4755756.html
Copyright © 2011-2022 走看看