LeetCode 054 Spiral Matrix
2016-06-23 22:58
127 查看
之前因为投paper停更了一段时间,现在继续更新。
题目大意:给出m行n列的矩阵,返回从外围到中间的螺旋序列。
按照圈数循环,然后每圈中依次访问4条边。
代码:
题目大意:给出m行n列的矩阵,返回从外围到中间的螺旋序列。
按照圈数循环,然后每圈中依次访问4条边。
代码:
vector<int> spiralOrder(vector<vector<int>>& matrix) { int m = matrix.size(); int n = 0; if(m != 0) n = matrix.front().size(); int m0 = 0; int n0 = 0; int m1 = m - 1; int n1 = n - 1; vector<int> result; while(m0 <= m1 && n0 <= n1) { for(int i = n0 ; i <= n1 ; i++) result.push_back(matrix[m0][i]); for(int i = m0 + 1 ; i <= m1 ; i++) result.push_back(matrix[i][n1]); if(m0 != m1) for(int i = n1 - 1 ; i >= n0 ; i--) result.push_back(matrix[m1][i]); if(n0 != n1) for(int i = m1 - 1 ; i >= m0 + 1 ; i--) result.push_back(matrix[i][n0]); m0++; n0++; m1--; n1--; } return result; }
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解