【leetcode刷题笔记】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].


题解:还是找规律的题:设有四个变量Xs,Xe,Ys,Ye,如下图所示,它们分别代表当前还未处理的子矩阵的边界,初始为图一所示;运算过程的某一时刻如图二所示

那么螺旋输出就是不停的重复以下四个步骤,直到矩阵所有的元素被输出:

1.输出matrix[Xs->Xe][Ys],Ys++  即1,2,3

2.输出matrix[Xe][Ys->Ye],Xe--  即6,9

3.输出matrix[Xe->Xs][Ye],Ye--  即8,7

4.输出matrix[Ye->Ys][Xs],Xs++  即4

代码如下:


 1 #include<iostream>
2 #include<vector>
3 using namespace std;
4 class Solution {
5 public:
6 vector<int> spiralOrder(vector<vector<int> > &matrix) {
7 vector<int> ans;
8
9 if(matrix.size() == 0)
10 return ans;
11
12 int Xs = 0,Xe = matrix[0].size()-1,Ys = 0,Ye = matrix.size()-1;
13 int total = (Xe+1) * (Ye+1);
14
15 for(int i = 0;i <= total;){
16 for(int k = Xs;k <= Xe;k++)
17 {
18 ans.push_back(matrix[Ys][k]);
19 i++;
20 }
21 if(i >= total)
22 break;
23 else
24 Ys++;
25
26 for(int k = Ys;k <= Ye;k++)
27 {
28 ans.push_back(matrix[k][Xe]);
29 i++;
30 }
31 if(i >= total)
32 break;
33 else
34 Xe--;
35
36 for(int k = Xe;k >= Xs;k--)
37 {
38 ans.push_back(matrix[Ye][k]);
39 i++;
40 }
41 if(i >= total)
42 break;
43 else
44 Ye--;
45
46 for(int k = Ye;k >= Ys;k--)
47 {
48 ans.push_back(matrix[k][Xs]);
49 i++;
50 }
51 if(i >= total)
52 break;
53 else
54 Xs++;
55 }
56 return ans;
57 }
58 };
59 int main(){
60 Solution s;
61 vector<vector<int> > m;
62 vector<int>temp(1,1);
63 m.push_back(temp);
64
65 vector<int> ans(s.spiralOrder(m));
66 cout <<ans.size()<<endl;
67 }

这里学习到了怎么接受函数返回回来的向量,直接定义新的向量初始化为函数返回值就可以了,如代码65行所示。

【leetcode刷题笔记】Spiral Matrix,码迷,mamicode.com

时间: 2024-10-11 10:31:04

【leetcode刷题笔记】Spiral Matrix的相关文章

【leetcode刷题笔记】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:以前做过的Spiral Matrix是给一个矩阵螺旋式的输出,这道题是给一个n,螺旋式的

【leetcode刷题笔记】Set Matrix Zeroes

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 题解:因为题目要求原地算法,所以我们只能利用矩阵第一行和第一列存放置零信息. 首先遍历第一行和第一列,看他们是否需要全部置零,用两个变量first_column_zero和first_row_zero来记录: 遍历矩阵,如果某个位置matrix[i][j]出现了0,就把matrix[i][0]和Matrix[0

【leetcode刷题笔记】Search a 2D Matrix

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro

【leetcode刷题笔记】Maximal Rectangle

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 题解,很巧妙的一道题,对于一个0-1矩阵,它的每一行及以上都可以看作一个直方图(如下图所示),利用Largest Rectangle in Histogram的方法,可以在O(n)的时间搜索出这一行及以上的直方图中面积最大的矩形,对矩阵的每一行依次做这个操作,就可

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

【leetcode刷题笔记】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

【leetcode刷题笔记】Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 题解: 设置两个变量:右边kepler和前向游标forward.如果当前kepeler所指的元素和

【leetcode刷题笔记】Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解:深度优先搜索.用resul

【leetcode刷题笔记】Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true, as t