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 ...
随机推荐
- python3下搜狗AI API实现
1.背景 a.搜狗也发布了自己的人工智能 api,包括身份证ocr.名片ocr.文本翻译等API,初试感觉准确率一般般. b.基于python3. c.也有自己的签名生成这块,有了鹅厂的底子,相对写起 ...
- FPGA与MATLAB数据交互高效率验证算法——仿真阶段
之前博文是对基本设计技巧的总结和一些小设计随笔,内容有点杂,缺乏目的性.本来后续计划设计几个小项目,但导师的任务比较紧,所以为了提高效率,后续博客会涉及到很多算法方面的设计与验证的内容,主要关于OFD ...
- React-Native(三):React Native是基于React设计的
React Native是基于React js设计的. 参考:<React 入门实例教程> React 起源于 Facebook 的内部项目,因为该公司对市场上所有 JavaScript ...
- 1.3WEB API 默认以json格式返回数据,同时定义时间格式,返回格式
首先我们知道,web api 是可以返回任意类型的,然后在输出的过程中转为(默认的)xml. 但是xml是比较费流量的,而且大多前端都是用json对接,所以我们也只能随大流,把它输出改成json. 不 ...
- Spring(5)——Spring 和数据库编程
传统 JDBC 回顾 JDBC 我们一定不陌生,刚开始学习的时候,我们写过很多很多重复的模板代码: public Student getOne(int id) { String sql = " ...
- jquery ajax 发送邮件例子
<div class="form"> <dl> <dt>您的称呼<small>(必填)</small></dt&g ...
- css 相关算法
计算 em 目标像素 除 基准像素 等于 em倍数结果: 14 / 16 = 0.875em 0.875倍也就是 14 像素 计算百分比 目标像素 除 父类总宽度 乘 一百:90 / 200 * 10 ...
- drupal8的安装
一.首先安装好linux系统虚拟机 1.在drupal官网上下载drupal包,https://www.drupal.org/download 我下载的是 https://ftp.drupal.org ...
- Spring Boot简单应用——会员管理系统
简介 本项目是使用Spring Boot编写的一个简单的会员管理系统. 提供了会员的解决方案,主要有会员模块,管理员模块,礼品模块,商品模块,会员等级模块,生日提醒模块,积分模块,详细模块如下图 准备 ...
- [BZOJ]4650: [Noi2016]优秀的拆分
Time Limit: 30 Sec Memory Limit: 512 MB Description 如果一个字符串可以被拆分为 AABBAABB 的形式,其中 AA 和 BB 是任意非空字符串, ...