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 题目链接 题意:这两题是一样的,仅仅是数据范围不一样,都是一个矩阵,从左上角走到右下角在从右下角走到左上角能得到最大价值 ...
随机推荐
- composer -vvv
然后在使用Composer install 或者 composer update 的时候会停住不动.使用-vvv可以输出更多信息,其命令参数输出的级别是Debug.具体可以查看composer hel ...
- Java中的异常处理
描述: 如果Java中的函数有可能抛出异常,则该异常要么被catch住,要么在声明函数时必须声明该函数体会throws exception. 处理的时候的流程是,当发生异常时,首先结束当前函数后续语句 ...
- MATLAB 例子研究 Motion-Based Multiple Object Tracking
这个例子是用来识别视频中多个物体运动的.我要研究的是:搞清楚识别的步骤和相应的算法,识别出物体运动的轨迹. 详细参见官方帮助文档,总结如下: 移动物体的识别算法:a background subtra ...
- C# 列主元素(Gauss)消去法 计算一元多次方程组
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- C#实现右下角弹出窗口效果
/// <summary> /// 窗体动画函数 注意:要引用System.Runtime.InteropServices; /// </summary> /// <pa ...
- 当一个activity中按钮过多时怎么办?
这几天看极客学院的视频,跟视频中的老师学到的一些小技巧~~ .setOnClickListener(this) 通过重写this(我猜的是重写),下面有onClicked() package exam ...
- plsql developer 导出导入存储过程和函数
说明:需要把建表脚本及表数据分开导出,操作很简单.一.导出表及存储过程等对象:1. 登录PL-SQL Developer2. 选择只显示本用户的对象,如下图:3. 选择菜单“Tools——〉Expor ...
- 【转】使用Sublime + PlantUML高效地画图
project: blog status: publish target: how-to-use-sublime-and-plant-uml-draw-diagram.md date: 2015-12 ...
- RabbitMQ/JAVA (路由选择)
上篇博文中,我们建立了一个简单的日志系统.可以广播消息给多个消费者.本篇博文,我们将添加新的特性--我们可以只订阅部分消息.比如:我们可以接收Error级别的消息写入文件.同时仍然可以在控制台打印所有 ...
- laravel_5《数据库迁移》
Laravel鼓励敏捷.迭代的开发方式,我们没指望在第一次就获得所有正确的.相反,我们编写代码.测试和与我们的最终用户进行交互,并完善我们的理解. 对于工作,我们需要一个配套的实践集.我们使用像sub ...