【leetcode】Spiral Matrix(middle)
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]
.
思路:就按照螺旋矩阵的规律 用n记录旋转圈数 每圈按左下右上的顺序取值。 注意去重复。
class Solution {
public:
vector<int> spiralOrder(vector<vector<int> > &matrix) {
vector<int> ans;
if(matrix.empty())
return ans;
int M = matrix.size();
int N = matrix[].size(); for(int n = ; n < min((M+)/,(N+)/);n++)
{
//行 ----->
for(int i = n; i < N - n; i++)
ans.push_back(matrix[n][i]);
//列 向下
for(int i = n + ; i < M - n; i++)
ans.push_back(matrix[i][N - - n]);
//行 <-----------
if(M - n - <= n) //列号 一定要比向左时的列号小 防止重复
break;
for(int i = N - n - ; i >= n; i--)
ans.push_back(matrix[M - n - ][i]);
//列 向上
if(n >= N - - n) //行号 一定要比向下时的行号大 防止重复
break;
for(int i = M - n - ; i >= n + ; i--)
ans.push_back(matrix[i][n]);
}
return ans;
}
};
大神思路和我一样,就是用自定义变量来避免重复取行或列。
public class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<Integer>();
if (matrix.length == ) {
return res;
}
int rowBegin = ;
int rowEnd = matrix.length-;
int colBegin = ;
int colEnd = matrix[].length - ; while (rowBegin <= rowEnd && colBegin <= colEnd) {
// Traverse Right
for (int j = colBegin; j <= colEnd; j ++) {
res.add(matrix[rowBegin][j]);
}
rowBegin++; // Traverse Down
for (int j = rowBegin; j <= rowEnd; j ++) {
res.add(matrix[j][colEnd]);
}
colEnd--; if (rowBegin <= rowEnd) {
// Traverse Left
for (int j = colEnd; j >= colBegin; j --) {
res.add(matrix[rowEnd][j]);
}
}
rowEnd--; if (colBegin <= colEnd) {
// Traver Up
for (int j = rowEnd; j >= rowBegin; j --) {
res.add(matrix[j][colBegin]);
}
}
colBegin ++;
} return res;
}
}
【leetcode】Spiral Matrix(middle)的更多相关文章
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【leetcode】Reorder List (middle)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- 【leetcode】Rotate Image(middle)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【leetcode】Course Schedule(middle)☆
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Rotate List(middle)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【leetcode】Partition List(middle)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【leetcode】Reverse Bits(middle)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
随机推荐
- Access应用笔记<三>
在搭了一个数据库后,买了一本<让excel飞>,里面有提及access 经过研究之后,发现access+excel的结合确实能解决我的大部分难题,并且可以做得更好. 比较好的方法是, ac ...
- nyoj 236心急的C小加 动态规划( java)
sort函数用法: #include<algorithm> using namespace std; sort(a,a+len;cmp) //a-->数组名, len-- ...
- VS2008上借助VA来提示QT API
1.打开VS,工具-->选项-->项目和解决方案-->VC++目录,在右侧下拉框中选择-->包含文件,添加QT的include目录 2. 打开VA配置页,Projects--& ...
- Elven Postman(BST )
Elven Postman Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- cpu利用率和cpu 队列
SIP的第四期结束了,因为控制策略的丰富,早先的的压力测试结果已经无法反映在高并发和高压力下SIP的运行状况,因此需要重新作压力测试.跟在测试人员后面做了快一周的压力测试,压力测试的报告也正式出炉,本 ...
- linux查找某一进程并杀死
1. 查找redis进程 ps -ef|grep redis-server 2.打印第二个参数,因为上面第二列是进程号 3.这两个进程号有一个是grep进程号,所以要去掉,反选 grep ps ...
- N-Queens leetcode
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- EF初接触01
自动属性:{get;set} 隐式类型 var, dynamic var: 隐式的类型推断出来,在编译阶段把Var换成对应的实际的类型 所以只应用在编译之间, 在运行阶段是和实际类型意义的 dyna ...
- EF自动生成的模型edmx代码分析
edmx代码分析 本文分析Entity Framework从数据库自动生成的模型文件代码(扩展名为edmx). 1. 概述 本文使用的数据库结构尽量简单,只有2个表,一个用户表和一个分公司表(相当于部 ...
- 使用JavaScript在项目前台开发的58种常用小技巧
oncontextmenu="return false" :禁止右键 onselectstart="return false" : 禁止选取 onpaste = ...