LeetCode OJ-- Spiral Matrix
https://oj.leetcode.com/problems/spiral-matrix/
螺旋矩阵,逆着转,输出矩阵中的元素。
在纸上模仿,然后记左上角(l1,l2)右上角(l1,r2),左下角(p1,l2)右下角(p1,r2).
然后4个for循环从一个点到另一个点位置遍历。
while控制总的。
当在一次遍历中,没有要输出的点,说明遍历结束。
class Solution {
public:
vector<int> spiralOrder(vector<vector<int> > &matrix) {
vector<int> ans;
int row = matrix.size();
if(row == )
return ans;
if(row ==)
{
for(int i = ;i<matrix[].size();i++)
ans.push_back(matrix[][i]);
return ans;
}
int col = matrix[].size();
int l1,l2,r2,p1;
l1 = ;
l2 = ;
r2 = col - ;
p1 = row -;
while()
{
int i;
if(l2>r2)
break;
for(i = l2; i <= r2; i++)
ans.push_back(matrix[l1][i]);
if(l1+>p1)
break;
for(i = l1+;i<= p1;i++)
ans.push_back(matrix[i][r2]);
if(r2-<l2)
break;
for(i = r2-;i>=l2;i--)
ans.push_back(matrix[p1][i]);
if(p1-<l1+)
break;
for(i = p1-;i>=l1+;i--)
ans.push_back(matrix[i][l2]);
l1++;
l2++;
r2--;
p1--;
}
return ans;
}
};
LeetCode OJ-- Spiral Matrix的更多相关文章
- Java for LeetCode 059 Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II
Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For ...
- [LeetCode 题解] Spiral Matrix
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 题目链接 54. Spiral Matrix ...
- LeetCode: 59. Spiral Matrix II(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...
- 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] 885. Spiral Matrix III 螺旋矩阵之三
On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...
- LeetCode 885. Spiral Matrix III
原题链接在这里:https://leetcode.com/problems/spiral-matrix-iii/ 题目: On a 2 dimensional grid with R rows and ...
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
随机推荐
- C#基础-循环语句
while语句 int i = 1,sum=0; while (i <= 100) { sum += i; i++; } Console.WriteLine(sum); do···while语句 ...
- vue_music:封装scroll.vue组件
在项目中经常用到滚动,结合Better-scroll封装了sroll.vue组件参考链接:https://ustbhuangyi.github.io...http://www.imooc.com/ar ...
- zend studio10 破解方法
zend studio 是一款强大的PHP开发工具,该软件的具体信息用户可以百度一下,下面是zend studio10的破解方法. 1.下载zend studio 10,下载地址:链接:http:// ...
- PHP GD库---之商详合成分享图片
$item_pic = 'img/item.jpg'; $qcode_pic = 'img/qcode.png'; $user_pic = 'img/user.jpeg'; $item_title = ...
- HDU 3861 The King’s Problem 强连通分量 最小路径覆盖
先找出强连通分量缩点,然后就是最小路径覆盖. 构造一个二分图,把每个点\(i\)拆成两个点\(X_i,Y_i\). 对于原图中的边\(u \to v\),在二分图添加一条边\(X_u \to Y_v\ ...
- UVa 11552 DP Fewest Flops
题解 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; ...
- configurationChanges
在Android中每次屏幕的切换动会重启Activity,所以应该在Activity销毁前保存当前活动的状态,在Activity再次Create的时候载入配置.在activity加上android:c ...
- Django 二——models(admin、ORM),一对一、一对多、多对多操作,all、values、value_list的对比
内容概要 1.关系对象映射ORM 2.admin的配置(选修) 3.all().values().value_list()的对比 4.数据库操作(一对一.一对多.多对多) 5.HttpResponse ...
- datatable 修改点击列头进行排序顺序
一般点击排序时,是先升序后降序 可以通过如下代码修改排序规则 jQuery(function ($) { $(".datatable").dataTable({ "pag ...
- [python工具][1]sublime安装与配置
http://www.cnblogs.com/wind128/p/4409422.html 1 官网下载版本 http://www.sublimetext.com/3 选择 Windows - al ...