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;
}
};
59. Spiral Matrix && Spiral Matrix II的更多相关文章
- leetcode@ [54/59] Spiral Matrix & Spiral Matrix II
https://leetcode.com/problems/spiral-matrix/ Given a matrix of m x n elements (m rows, n columns), r ...
- 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 follo ...
- Android图片旋转,缩放,位移,倾斜,对称完整示例(一)——imageView.setImageMatrix(matrix)和Matrix
MainActivity如下: import android.os.Bundle; import android.view.MotionEvent; import android.view.View; ...
- Android图片旋转,缩放,位移,倾斜,对称完整演示样例(一)——imageView.setImageMatrix(matrix)和Matrix
MainActivity例如以下: import android.os.Bundle; import android.view.MotionEvent; import android.view.Vie ...
- Pramp mock interview (4th practice): Matrix Spiral Print
March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...
- LeetCode(59):螺旋矩阵 II
Medium! 题目描述: 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, ...
- 方差variance, 协方差covariance, 协方差矩阵covariance matrix | scatter matrix | weighted covariance | Eigenvalues and eigenvectors
covariance, co本能的想到双变量,用于描述两个变量之间的关系. correlation,相关性,covariance标准化后就是correlation. covariance的定义: 期望 ...
- Directx Matrix.PerspectiveFovLH Matrix.PerspectiveFovRH的理解
该函数一个四个参数public static Matrix PerspectiveFovLH ( float fieldOfViewY, float aspectRatio, float znearP ...
- HDU 2686 Matrix 3376 Matrix Again(费用流)
HDU 2686 Matrix 题目链接 3376 Matrix Again 题目链接 题意:这两题是一样的,仅仅是数据范围不一样,都是一个矩阵,从左上角走到右下角在从右下角走到左上角能得到最大价值 ...
随机推荐
- Codeforces Round #382 (Div. 2) 解题报告
CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...
- iframe自定义高度
function setIframeHeight() { var iframe=document.getElementById("iframe_id"); iframe.heigh ...
- web前端基础篇①
html1.html5和css3在IE上都不支持2.超过1100px以上的宽度,就会出现滚动条3.<!doctype html>解析模式 分为 标准模式 混杂模式 非标准模式-针对IE6o ...
- C#实现右下角弹出窗口效果
/// <summary> /// 窗体动画函数 注意:要引用System.Runtime.InteropServices; /// </summary> /// <pa ...
- python多线程监控指定目录
import win32file import tempfile import threading import win32con import os dirs=["C:\\WINDOWS\ ...
- 【C++ STL编程】queue小例子
STL是标准化组件,现在已经是C++的一部分,因此不用额外安装什么. #include <queue> #include <iostream> using namespace ...
- javaweb---html标签
img标签
- angular插件合集
图片视频类 angular-maxonry 图片墙效果插件,可以将图片组织成类似于瀑布流的效果,依赖于jQuery.imageloaded和Masonry angular-deckgrid 另一个照片 ...
- 关于谷歌浏览器 表单元素获取焦点后自动增加外边线的问题解决CSS代码
input,textarea:focus { outline: none;} /*去除表单元素默认边框*/
- 三部曲一(搜索、数学)-1016-Code
Code Time Limit : 2000/1000ms (Java/Other) Memory Limit : 60000/30000K (Java/Other) Total Submissi ...