59. Spiral Matrix && Spiral Matrix II

Spiral Matrix

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].

思路: 可参考剑指offer:题20

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int> > &matrix) {
        vector<int> vec;
        if(!matrix.size() || !matrix[0].size()) return vec;
        int row = matrix.size(), col = matrix[0].size();
        int rl = 0, rh = row-1, cl = 0, ch = col-1;
        while(rh >= rl && ch >= cl) {
            for(int c = cl; c <= ch; ++c) vec.push_back(matrix[rl][c]);
            if(++rl > rh) break;
            for(int r = rl; r <= rh; ++r) vec.push_back(matrix[r][ch]);
            if(--ch < cl) break;
            for(int c = ch; c >= cl; --c) vec.push_back(matrix[rh][c]);
            --rh;
            for(int r = rh; r >= rl; --r) vec.push_back(matrix[r][cl]);
            ++cl;
        }
        return vec;
    }
};

Spiral Matrix II

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 ]
]
class Solution {
public:
    vector<vector<int> > generateMatrix(int n) {
        vector<vector<int> > vec(n, vector<int>(n));
        int rl = 0, rh = n-1, cl = 0, ch = n-1, v = 1;
        while(rh >= rl && ch >= cl) {
            for(int c = cl; c <= ch; ++c) vec[rl][c] = v++;
            if(++rl > rh) break;
            for(int r = rl; r <= rh; ++r) vec[r][ch] = v++;
            if(--ch < cl) break;
            for(int c = ch; c >= cl; --c) vec[rh][c] = v++;
            --rh;
            for(int r = rh; r >= rl; --r) vec[r][cl] = v++;
            ++cl;
        }
        return vec;
    }
};
时间: 2024-08-03 22:04:54

59. Spiral Matrix && Spiral Matrix II的相关文章

Print matrix spiral

Problem Print a matrix in spiral fashion. Solution We will first print the periphery of the matrix by the help of 4 for loops. Then recursively call this function to do the same thing with inner concentric rectangles. We will pass this information by

Leetcode 74 and 240. Search a 2D matrix I and II

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro

Android图片旋转,缩放,位移,倾斜,对称完整演示样例(一)——imageView.setImageMatrix(matrix)和Matrix

MainActivity例如以下: import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ImageView; import android.app.Activity; import android.graphics.Matrix; /** * Demo描

Android图片旋转,缩放,位移,倾斜,对称完整示例(一)——imageView.setImageMatrix(matrix)和Matrix

MainActivity如下: import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ImageView; import android.app.Activity; import android.graphics.Matrix; /** * Demo描述:

Spiral Matrix &amp;&amp; Spiral Matrix II

Spiral Matrix 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]. 解题思路: 求一个

Directx Matrix.PerspectiveFovLH Matrix.PerspectiveFovRH的理解

该函数一个四个参数public static Matrix PerspectiveFovLH ( float fieldOfViewY, float aspectRatio, float znearPlane, float zfarPlane ) 其中参数 fieldOfViewY 表示视场在 Y 方向的弧度,参数 aspectRatio 表示平面纵横比,参数 znearPlane,表示近平面的距离,参数 zfarPlane 表示远平面的距离. 进行绘制的时候,只有处于近平面和远平面之间的物体才

HDU 2686 Matrix 3376 Matrix Again(费用流)

HDU 2686 Matrix 题目链接 3376 Matrix Again 题目链接 题意:这两题是一样的,只是数据范围不一样,都是一个矩阵,从左上角走到右下角在从右下角走到左上角能得到最大价值 思路:拆点,建图,然后跑费用流即可,不过HDU3376这题,极限情况是300W条边,然后卡时间过了2333 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> #i

一步一步学习S-MSCKF(五)观测更新 \left[\begin{matrix} \end{matrix}\right]

测量模型 考虑一个特征点\(f_j\)被位置在\((^{C_i}_Gq,{^{G}p}_{C_i})\)的双目相机观测到.双目的左右相机位姿可以表示为\((^{C_{i,1}}_Gq,^{G}p_{C_{i,1}})\)和\((^{C_{i,2}}_Gq,^{G}p_{C_{i,2}})\).虽然状态向量仅仅包含左相机的位姿,但是相机左目与右目的位姿可以通过双目相机的外参产生联系. 双目的观测值(归一化坐标)可以表达为: \[z^{j}_i= \left[\begin{matrix} u_{i,

Leetcode 54. Spiral Matrix &amp; 59. Spiral Matrix II

54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7,4,5] Example 2: Input: