LeetCode 54/59. Spiral Matrix i, ii
2016-04-26 20:51
495 查看
1. 题目描述
54Given 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].
59
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 ]
]
2. 解题思路
这两道题目非常类似, 处理的思路也是基本雷同, 只需要处理下访问一周的数据方法, 即可3. code
3.1 54
class Solution { public: vector<int> spiralOrder(vector<vector<int>>& matrix) { vector<int> res; if (matrix.size() == 0) return res; int m = matrix.size(); int n = matrix[0].size(); for (int i = 0; i != (min(m, n) + 1) / 2; i++) search_around(matrix, m, n, i, res); return res; } private: void search_around(vector<vector<int>> & arr, int m, int n, int offset, vector<int> & res){ if (offset == m - offset - 1 && m <= n){ for (int i = offset; i != n - offset; i++) res.push_back(arr[offset][i]); return; } if (offset == n - offset - 1 && m >= n){ for (int i = offset; i != m - offset; i++) res.push_back(arr[i][offset]); return; } for (int j = offset; j != n - offset - 1; j++) res.push_back(arr[offset][j]); for (int i = offset; i != m - offset - 1; i++) res.push_back(arr[i][n - offset - 1]); for (int j = n - offset - 1; j != offset; j--) res.push_back(arr[m - offset - 1][j]); for (int i = m - offset - 1; i != offset; i--) res.push_back(arr[i][offset]); } };
3.2 59
class Solution { public: vector<vector<int>> generateMatrix(int n) { vector<vector<int>> arr(n, vector<int>(n, 0)); int cur_depth = 1; for (int i = 0; i != (n + 1) / 2; i++) search_around(arr, n, cur_depth, i); return arr; } private: void search_around(vector<vector<int>> & arr, int n, int & cur_depth, int offset){ if (offset == n - offset - 1){ arr[offset][offset] = cur_depth++; return; } for (int j = offset; j != n - offset - 1; j++) arr[offset][j] = cur_depth++; for (int i = offset; i != n - offset - 1; i++) arr[i][n - offset - 1] = cur_depth++; for (int j = n - offset - 1; j != offset; j--) arr[n - offset - 1][j] = cur_depth++; for (int i = n - offset - 1; i != offset; i--) arr[i][offset] = cur_depth++; } };
4. 大神解法
4.2 54
一样的思想, 不过感觉个人将 访问一周的数据独立成一个函数更加容易理解一些/* This is a very simple and easy to understand solution. I traverse right and increment rowBegin, then traverse down and decrement colEnd, then I traverse left and decrement rowEnd, and finally I traverse up and increment colBegin. The only tricky part is that when I traverse left or up I have to check whether the row or col still exists to prevent duplicates. If anyone can do the same thing without that check, please let me know! Any comments greatly appreciated. */ public class Solution { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> res = new ArrayList<Integer>(); if (matrix.length == 0) { return res; } int rowBegin = 0; int rowEnd = matrix.length-1; int colBegin = 0; int colEnd = matrix[0].length - 1; while (rowBegin <= rowEnd && colBegin <= colEnd) { // Traverse Right for (int j = colBegin; j <= colEnd; j ++) { res.add(matrix[rowBegin][j]); } rowBegin++; // Traverse Down for (int j = rowBegin; j <= rowEnd; j ++) { res.add(matrix[j][colEnd]); } colEnd--; if (rowBegin <= rowEnd) { // Traverse Left for (int j = colEnd; j >= colBegin; j --) { res.add(matrix[rowEnd][j]); } } rowEnd--; if (colBegin <= colEnd) { // Traver Up for (int j = rowEnd; j >= rowBegin; j --) { res.add(matrix[j][colBegin]); } } colBegin ++; } return res; } }
4.2 59
clean logicclass Solution { public: vector<vector<int> > generateMatrix(int n) { vector<vector<int> > ret( n, vector<int>(n) ); int k = 1, i = 0; while( k <= n * n ) { int j = i; // four steps while( j < n - i ) // 1. horizonal, left to right ret[i][j++] = k++; j = i + 1; while( j < n - i ) // 2. vertical, top to bottom ret[j++][n-i-1] = k++; j = n - i - 2; while( j > i ) // 3. horizonal, right to left ret[n-i-1][j--] = k++; j = n - i - 1; while( j > i ) // 4. vertical, bottom to top ret[j--][i] = k++; i++; // next loop } return ret; } };
相关文章推荐
- 18. 4Sum
- 27. Remove Element
- Html5之Full Screen API
- C++作业4
- 在虚拟机扩展linux系统的硬盘
- 25,Spark Sort-Based Shuffle内幕彻底解密
- BZOJ 3396 水流
- LeeCode(Database)-Combine Two Tables
- Letter Combinations of a Phone Number
- 二维树状数组学习
- YOLO image结构体操作学习
- 58. Length of Last Word
- 从原理看Kruskal与Prim求最小生成树
- 读写锁
- SpringMVC之HiddenHttpMethodFilter 过滤器
- 笔试题之Java基础部分
- 用两个栈实现队列的操作
- 后台json数据为空,解析 出现空指针异常
- leetcode 043 Multiply Strings
- 装苹果