54. 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].
下面的代码是这样办的: 1 2 3, 6 9, 8 7, 4, 5.
题意为旋转输出矩阵元素.
人家想法: travel路线: right -> down -> left -> up 循环直到违反循环条件(rb <= re && cb <= ce)为止.
重点是控制 row_begin, row_end, col_begin and col_end. 注意循环条件,以及 left 和 up 时的条件判断.
下面代码展现的思想特清楚.
人个想法,自个代码:
\(O(n^2)\) time, \(O(1)\) extra space.
vector<int> spiralOrder(vector<vector<int>>& A) {
vector<int> res;
if (A.size() == 0) return res;
const int m = A.size(), n = A[0].size();
int rb = 0, re = m - 1, cb = 0, ce = n - 1;
while (rb <= re && cb <= ce) {
// right travel
for (int j = cb; j <= ce; j++)
res.push_back(A[rb][j]);
rb++;
// down travel
for (int j = rb; j <= re; j++)
res.push_back(A[j][ce]);
ce--;
// left travel
if (rb <= re) {
for (int j = ce; j >= cb; j--)
res.push_back(A[re][j]);
}
re--;
// up travel
if (cb <= ce) {
for (int j = re; j >= rb; j--)
res.push_back(A[j][cb]);
}
cb++;
}
return res;
}
54. Spiral Matrix(中等)的更多相关文章
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- [Leetcode][Python]54: Spiral Matrix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 54: Spiral Matrixhttps://leetcode.com/p ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- Leetcode 54. Spiral Matrix & 59. Spiral Matrix II
54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...
- Leetcode 54:Spiral Matrix 螺旋矩阵
54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...
- [array] leetcode - 54. Spiral Matrix - Medium
leetcode-54. Spiral Matrix - Medium descrition GGiven a matrix of m x n elements (m rows, n columns) ...
- leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- 54. Spiral Matrix
题目: Given a matrix of m x n elements (m rows, ncolumns), return all elements of the matrix in spiral ...
- LeetCode OJ 54. Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
随机推荐
- 新概念英语(1-113)Small Change
Lesson 113 Small Change 零钱 Listen to the tape then answer this question. Who has got some change?听录音 ...
- POJ2398【判断点在直线哪一侧+二分查找区间】
题意:同POJ2318 #include<algorithm> #include<cstdio> #include<cstdlib> #include<cst ...
- requests+正则爬取豆瓣图书
#requests+正则爬取豆瓣图书 import requests import re def get_html(url): headers = {'User-Agent':'Mozilla/5.0 ...
- 使用vue-cli快速搭建大型单页面应用开发环境
工作环境:terminal,已经全局安装了vue(可使用npm install -g vue) 全局安装vue-cli npm install -g vue-cli 创建一个基于webpack模板的项 ...
- jvm学习记录-对象的创建、对象的内存布局、对象的访问定位
简述 今天继续写<深入理解java虚拟机>的对象创建的理解.这次和上次隔的时间有些长,是因为有些东西确实不好理解,就查阅各种资料,然后弄明白了才来做记录. (此文中所阐述的内容都是以Hot ...
- CentOS 7 快速初始化脚本 for MySQL
#!/bin/bash## CentOS 7.x # SSH configuresshd_port=22 # Disable SElinuxprintf "Disable SElinux.. ...
- Apache 安装与配置(WIN10)
本地坏境:windows 10 Pro 1709 Apache版本:httpd-2.4.32-Win64-VC15 Apache下载地址:https://www.apachelounge.com/do ...
- CSS常用字体属性(多出的文本隐藏,或者以省略号的形式显示)和背景样式以及背景图的2个不常用属性:background-origin和background-clip
(一)常用的字体属性: font-weight: 属性值100-900 400等于正常 700等于bold ,数值越大,越粗 font-size:字体大小,单位可以为px或者% font-famil ...
- codevs 搜索题汇总(黄金级)
2801 LOL-盖伦的蹲草计划 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题目描述 Description 众所周知,LOL这款伟大的游戏,有个叫盖 ...
- codevs 2621 土地侵蚀
提交地址:http://codevs.cn/problem/2621/ 2621 土地侵蚀 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题目描述 De ...