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 ...
随机推荐
- HTTP协议的消息头:Content-Type和Accept的作用
一.背景知识 1.概述 Http报头分为通用报头,请求报头,响应报头和实体报头. 请求方的http报头结构:通用报头|请求报头|实体报头 响应方的http报头结构:通用报头|响应报头|实体报头 Acc ...
- 阿里云API网关(8)开发指南-SDK下载
网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...
- python学习之路01
python自己也自学过一段时间了,看过视频,也买过几本基础的书来看,目前为止对于一些简单的代码还是可以看懂,但是自己总是觉得缺少些什么,可能是缺少系统化的学习,也可能是缺少实际项目经验,对于这些缺少 ...
- bs4解析要获取被注掉的部分需先将注释符号去掉
<div class="xzcf-content"> <div id="sfxz"> <div class="main- ...
- 最短路径之Dijkstra算法
Dijkstra(迪杰斯特拉)算法是典型的最短路径路由算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Dijkstra算法能得出最短路径的最优 ...
- Ubuntu Sublime 配置
p { margin-bottom: 0.25cm; line-height: 120% } a:link { } 2018.4.14 Ubuntu Sublime 配置 承 Ubuntu Apach ...
- HBase表创建、删除、清空
HBase shell窗口进入 执行命令hbase shell HBase表的创建 # 语法:create <table>, {NAME => <family>, VER ...
- 【Android】Android Studio3.1 Mac版本设置项目桌面icon
近来项目处于测试阶段,工作少了许多,就装了个最新的Android Studio,想写一下安卓.新建好项目,想设置个桌面的icon.我先准备好自己的icon图片,然后复制粘贴到res/mipmap-hd ...
- python九九乘法表
j = 1 while j <= 9: i = 1 while i <= j: print("%d*%d=%d\t" % (i, j, i*j), end=" ...
- [Tjoi 2013]松鼠聚会
3170: [Tjoi 2013]松鼠聚会 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1318 Solved: 664[Submit][Stat ...