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)的更多相关文章

  1. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  2. 【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 ...

  3. 【leetcode】Rotate Image(middle)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  4. 【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 ...

  5. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  6. 【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 ...

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

  8. 【leetcode】Next Permutation(middle)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  9. 【leetcode】Reverse Bits(middle)

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

随机推荐

  1. 基础知识系列☞C#中→属性和字段的区别

    "好吧...准备写个'基础知识系列',算是记录下吧,时时看看,更加加深记忆···" 其实本来准备叫"面试系列"... 字段.属性.你先知道的哪个概念? ***我 ...

  2. nyoj 4 ASCII码排序 java

    java输入字符:1.String s=sc.next(); 2.char a=s.charAt(0); 注意:package   java 中提交不能带package java代码: import ...

  3. Mac Pro 安装 Homebrew 软件包管理工具

    Linux系统有个让人蛋疼的通病,软件包依赖,好在当前主流的两大发行版本都自带了解决方案,Red hat有 yum,Ubuntu有 apt-get. Mac os 中没有类似的东东,不过有第三方库支持 ...

  4. 改造 ThinkPHP,弃用 D() 等魔术函数

    ThinkPHP 是国内比较优秀的 PHP 框架,但有些地方不是很好,比如那些 魔术函数 D(),用它返回的类实例,在各个IDE(如 PhpStorm)下根本识别不了,导致如下问题: 1.不支持 代码 ...

  5. WPF:自定义路由事件的实现

    路由事件通过EventManager,RegisterRoutedEvent方法注册,通过AddHandler和RemoveHandler来关联和解除关联的事件处理函数:通过RaiseEvent方法来 ...

  6. Java Io 之 编码

    Java字符串编码一些知识总结: package com.dcz.io; import java.io.UnsupportedEncodingException; public class Encod ...

  7. 说说JSON和JSONP,也许你会豁然开朗

    前言 由于Sencha Touch 2这种开发模式的特性,基本决定了它原生的数据交互行为几乎只能通过AJAX来实现. 当然了,通过调用强大的PhoneGap插件然后打包,你可以实现100%的Socke ...

  8. Sqli-LABS通关笔录-9[延时注入]

    通过这个关卡 1.更快的掌握到了如何判断是否是延时注入 无论咋输入,都不行.当payload为: http://127.0.0.1/sql/Less-9/index.php?id=1' and sle ...

  9. hiho #1284 机会渺茫

    #1284 : 机会渺茫 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi最近在追求一名学数学的女生小Z.小Z其实是想拒绝他的,但是找不到好的说辞,于是提出了这样的要 ...

  10. MyEclipse 优化

    1.取消自动validation 有一堆,什么xml.jsp.jsf.js等等, 我们没有必要全部都去自动校验一下,只是需要的时候才会手工校验一下! 取消方法: windows-->perfer ...