**一. 題目描述**
Given a matrix ofmn elements (mrows, 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]`.
**二. 題目分析**
題意:給定一個m*n的矩陣,從外圍一層一層的打印出矩陣中的元素內容。解題的方法有多種,以下采用的方法是,使用四個數字分別記錄上下左右四個邊界的位置(beginX,endX,beginY,endY),不斷循環以收窄這些邊界,最終當兩個邊界重疊時,結束循環。
同時,當循環完成后,即得到所求數組。
**三. 示例代碼**
~~~
class Solution
{
public:
vector<int> spiralOrder(vector<vector<int> >& matrix) {
vector<int> result;
if (matrix.empty()) return result;
int beginX = 0, endX = matrix[0].size() - 1;
int beginY = 0, endY = matrix.size() - 1;
while (1) {
// 從左到右
for (int i = beginX; i <= endX; ++i)
result.push_back(matrix[beginY][i]);
if (++beginY > endY) break;
// 從上到下
for (int i = beginY; i <= endY; ++i)
result.push_back(matrix[i][endX]);
if (beginX > --endX) break;
// 從右到左
for (int i = endX; i >= beginX; --i)
result.push_back(matrix[endY][i]);
if (beginY > --endY) break;
// 從下到上
for (int i = endY; i >= beginY; --i)
result.push_back(matrix[i][beginX]);
if (++beginX > endX) break;
}
return result;
}
};
~~~
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016騰訊軟件開發筆試題)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契數列問題)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波蘭式的計算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和問題)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching