LeetCode: Spiral Matrix 解题报告
2014-10-23 18:39
309 查看
[b]
View Code
[b][b]GitHub CODE: [/b]
[b]spiralOrder.java
[/b]
public List<Integer> spiralOrder3(int[][] matrix) { List<Integer> ret = new ArrayList<Integer>(); if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { return ret; } int rows = matrix.length; int cols = matrix[0].length; int left = 0; int right = cols - 1; int top = 0; int bottom = rows - 1; while (left <= right && top <= bottom) { // line top. for (int i = left; i <= right; i++) { ret.add(matrix[top][i]); } // line right; for (int i = top + 1; i <= bottom - 1; i++) { ret.add(matrix[i][right]); } // line bottom. if (top != bottom) { for (int i = right; i >= left; i--) { ret.add(matrix[bottom][i]); } } // line left; if (left != right) { for (int i = bottom - 1; i >= top + 1; i--) { ret.add(matrix[i][left]); } } left++; right--; top++; bottom--; } return ret; }
View Code
[b][b]GitHub CODE: [/b]
[b]spiralOrder.java
[/b]
相关文章推荐
- [LeetCode] Spiral Matrix 解题报告
- LeetCode: Spiral Matrix 解题报告
- [LeetCode]Spiral Matrix,解题报告
- [LeetCode] Spiral Matrix 解题报告
- LeetCode解题报告—— Group Anagrams & Pow(x, n) & Spiral Matrix
- leetcode 378. Kth Smallest Element in a Sorted Matrix 解题报告
- [Leetcode] 378. Kth Smallest Element in a Sorted Matrix 解题报告
- [leetcode] 329. Longest Increasing Path in a Matrix 解题报告
- 【LeetCode】Spiral Matrix解题笔记
- Leetcode 74. Search a 2D Matrix 2D矩阵查找 解题报告
- 【LeetCode】01 Matrix 解题报告
- [Leetcode] 73. Set Matrix Zeroes 解题报告
- LeetCode74 Search a 2D Matrix 解题报告
- leetcode 329. Longest Increasing Path in a Matrix 解题报告
- 【LeetCode】Search a 2D Matrix 解题报告
- LeetCode: Set Matrix Zeroes 解题报告
- LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors
- 【LeetCode】74. Search a 2D Matrix 解题报告(Python)
- 【LeetCode】Set Matrix Zeroes 解题报告
- [Leetcode] 566. Reshape the Matrix 解题报告