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

解题思路:

求一个矩阵循环绕矩形输出元素。我们可以利用矩阵四条边的规则输出各个元素。左上---右上,右上---右下,右下---左下,左下---左上,按此顺序输出矩形的四条边,矩形由小到大,直到输出所有元素为止。代码如下:

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        vector<int> res;
        if(matrix.size()==0) return res;
        int n=matrix.size();
	int m=matrix[0].size();

	for (int row=0;row<n/2+1;row++)
	{
		int i=row;
		int j=i;
		for (j=i;j<(m-row);j++)
		{
			res.push_back(matrix[i][j]);
			if (res.size()==(n*m))
			{
				return res;
			}
		}
		j--;
		for (i=row+1;i<(n-row);i++)
		{
			res.push_back(matrix[i][j]);
			if (res.size()==(n*m))
			{
				return res;
			}
		}
		i--;
		for (j=j-1;j>=row;j--)
		{
			res.push_back(matrix[i][j]);
			if (res.size()==(n*m))
			{
				return res;
			}
		}
		j++;
		for (i=i-1;i>=row+1;i--)
		{
			res.push_back(matrix[i][j]);
			if (res.size()==(n*m))
			{
				return res;
			}
		}
	}
	return res;

    }
};

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) {
        int x=0;
	vector<vector<int>> res;
	vector<int> temp(n,0);
	for (int i=0;i<n;i++)
	{
		res.push_back(temp);
	}
	for (int row=0;row<n/2+1;row++)
	{
		int i=row;
		int j=i;
		for (j=i;j<(n-row);j++)//左---右
		{
			res[i][j]=++x;
		}
		j--;
		for (i=row+1;i<(n-row);i++)//上---下
		{
			res[i][j]=++x;
		}
		i--;
		for (j=j-1;j>=row;j--)//右---左
		{
			res[i][j]=++x;
		}
		j++;
		for (i=i-1;i>=row+1;i--)//下---上
		{
			res[i][j]=++x;
		}
	}
	return res;

    }
};
时间: 2024-10-27 18:59:34

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

59. 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]. 思路: 可参考剑指

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描述:

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,

Spiral Matrix II

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 ] ] 这道题接着上面那道题,是一个逆过程,只要稍微改一下就好了,因