[LeetCode] 59. Spiral Matrix II
2016-07-26 18:18
375 查看
思路:
和Spiral Matrix I思路一模一样, 请参考http://blog.csdn.net/hiimdaosui/article/details/52035391
和Spiral Matrix I思路一模一样, 请参考http://blog.csdn.net/hiimdaosui/article/details/52035391
vector<vector<int>> generateMatrix(int n) { vector<vector<int>> res(n, vector<int>(n, 0)); if (! n) return res; int count = 0, direction = 0; int left = 0, top = 0, right = n - 1, bot = n - 1; int row = 0, col = 0; while (count++ < n * n) { res[row][col] = count; switch(direction) { case 0: if (col == right) { top++; row++; direction = 1; } else col++; break; case 1: if (row == bot) { right--; col--; direction = 2; } else row++; break; case 2: if (col == left) { bot--; row--; direction = 3; } else col--; break; case 3: if (row == top) { left++; col++; direction = 0; } else row--; break; } } return res; }
相关文章推荐
- C#使用Matrix执行缩放的方法
- 如何使用Matrix对bitmap的旋转与镜像水平垂直翻转
- 如何利用matrix实现图片倒影效果
- Android中Matrix用法实例分析
- Android中使用Matrix控制图形变换和制作倒影效果的方法
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- HP Z800 Workstation - Configuring RAID devices (HP Z800 做磁盘Raid1,Raid0的阵列)
- Android Matrix类以及ColorMatrix类详解
- leetcode----Longest Substring Without Repeating Characters
- 用css实现图片的旋转
- 让舞台上的按钮btn灰掉
- Android中Bitmap和Drawable
- [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