054 Spiral Matrix 旋转打印矩阵
给出一个 m x n 的矩阵(m 行, n 列),请按照顺时针螺旋顺序返回元素。
例如,给出以下矩阵:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
应该返回 [1,2,3,6,9,8,7,4,5]。
详见:https://leetcode.com/problems/spiral-matrix/description/
Java实现:
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res=new ArrayList<Integer>();
if(matrix==null||matrix.length==0){
return res;
}
int row=matrix.length;
int col=matrix[0].length; int top=0;
int bottom=row-1;
int left=0;
int right=col-1;
while(top<=bottom&&left<=right){
for(int j=left;j<=right;++j){
res.add(matrix[top][j]);
}
++top;
for(int i=top;i<=bottom;++i){
res.add(matrix[i][right]);
}
--right;
if(top<=bottom){
for(int j=right;j>=left;--j){
res.add(matrix[bottom][j]);
}
}
--bottom;
if(left<=right){
for(int i=bottom;i>=top;--i){
res.add(matrix[i][left]);
}
}
++left;
}
return res;
}
}
C++实现:
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> res;
if (matrix.empty())
return res; int row = matrix.size();
int col = matrix[0].size(); int top = 0;
int bottom = row - 1;
int left = 0;
int right = col - 1; //螺旋曲线,运动轨迹总是一致的
while (top <= bottom && left <= right)
{
//向右列递增遍历
for (int j = left; j <= right; j++)
{
res.push_back(matrix[top][j]);
}
top++; //遍历后,去掉此行 //向下行递增遍历
for (int i = top; i <= bottom; i++)
{
res.push_back(matrix[i][right]);
}
right--; //遍历后,去掉此列 if (top <= bottom) //重要判断,防止重复
{
//向左列递减遍历
for (int j = right; j >= left; j--)
{
res.push_back(matrix[bottom][j]);
} }
bottom--; //遍历后,去掉此行 if (left <= right) //重要判断,防止重复
{
//向上行递减遍历
for (int i = bottom; i >= top; i--)
{
res.push_back(matrix[i][left]);
}
}
left++; //遍历后,去掉此列
} return res;
}
};
054 Spiral Matrix 旋转打印矩阵的更多相关文章
- [Leetcode] spiral matrix ii 螺旋矩阵
Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...
- [LeetCode] 885. Spiral Matrix III 螺旋矩阵之三
On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...
- PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]
1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...
- [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II
Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For ...
- [LeetCode] Spiral Matrix II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 059 Spiral Matrix II 旋转打印矩阵 II
给出正整数 n,生成正方形矩阵,矩阵元素为 1 到 n2 ,元素按顺时针顺序螺旋排列.例如,给定正整数 n = 3,应返回如下矩阵:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6 ...
- 【LeetCode每天一题】Spiral Matrix(螺旋打印数组)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- LeetCode 54. Spiral Matrix(螺旋矩阵)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 054. Spiral Matrix
题目链接:https://leetcode.com/problems/spiral-matrix/description/ Given a matrix of m x n elements (m ro ...
随机推荐
- struts2框架xml验证
struts2验证分为3步: 1.获取需要验证的信息,使用同名属性,提供getter,setter方法.然后框架使用反射将值自动注入. 2.对信息进行验证,成功失败作出对应的选择. xml验证和手动验 ...
- win7 jenkins 修改主目录
1.安装tomcat 2.下载Jenkins.war包,把Jenkins.war放在D:\01Install\tomcat\webapps目录下,启动tomcat
- 善用搜索--->描述问题 [关于SwipeRefreshLayout]
遇到了一个问题,SwipeRefreshLayout没法在加载listView之前呈现progressBar.我一直在想,是不是只能在listView加载出来才能呈现它. 发邮件问了一个开发者,他说他 ...
- ACM学习历程—ZOJ 3861 Valid Pattern Lock(dfs)
Description Pattern lock security is generally used in Android handsets instead of a password. The p ...
- 连续4个小时ping不通远端主机,则本机关机
#!/bin/bash #连续4个小时ping不通远端主机,则本机关机. begin_time=$(date "+%s") while true;do sleep 2 ping - ...
- 2012年浙大:Head of a Gang
题目描述: One way that the police finds the head of a gang is to check people's phone calls. If there is ...
- Ubuntu ssh免密登录
ssh免密登录工作原理 server A免登录到server B: 1.在A上生成公钥私钥. 2.将公钥拷贝给server B,要重命名成authorized_keys(从英文名就知道含义了) 3.S ...
- 响应式web设计,html5和css3实战(@author Ben Fraim)
重定向 //请求重定向简化写法 response.sendRedirect("/day09/adv.html"); 转发 request.getRequestDispatcher( ...
- SimpliciTI使用
SimpliciTI组网过程介绍 1.SimpliciTI支持点对点和星形的网络拓扑结构. 下面介绍以AP为中心的SimpliciTI网路协议的星形拓扑结构通信过程 1)当ED节点上电之后就扫描信 ...
- Brute-Force-Attack on Triple-DES with Reduced Key Space
题目地址:https://www.mysterytwisterc3.org/en/challenges/level-ii/brute-force-attack-on-triple-des-with-r ...