Leetcode 059
来源:互联网 发布:围棋棋力测试软件 编辑:程序博客网 时间:2024/06/17 12:26
Total Accepted: 48287 Total Submissions: 141170 Difficulty: Medium
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n = 3
,
[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]
循环填数,从左右至右再到下到左,到上 ;因此模拟即可 ;
class Solution {public: vector<vector<int>> generateMatrix(int n) { vector<vector<int>>res(n,vector<int>(n)); if (n == 0 ) return res ; int x = 0 , y = 0 ; int cur = 1 ; // memset(res , 0 ,sizeof(res) ) ; res[0][0] = 1 ; while ( cur < n*n ) { while (y+1 < n && !res[x][y+1]) res[x][++y] = ++cur ; while (x+1 < n && !res[x+1][y]) res[++x][y] = ++cur ; while (y-1 >=0 && !res[x][y-1]) res[x][--y] = ++cur ; while (x-1 >=0 && !res[x-1][y]) res[--x][y] = ++cur ; } return res ; } };
0 0
- Leetcode 059
- LeetCode 059 Spiral MatrixII
- LeetCode: Permutation Sequence [059]
- [LeetCode] 059: Multiply Strings
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- 深入分析DWR的Engine._execute方法
- 一道计数DP
- jquery获取标签中的值
- Spring AOP 实例
- 大小端机器的判定
- Leetcode 059
- HTML5-localStorage、sessionStorage用法总结
- iOS ,不提交审核,修复某些线上bug以及线上更新
- 泛型算法——quick sort
- angularjs $broadcast $emit $on 事件触发controller间的值传递
- 分布式服务框架 Zookeeper -- 管理分布式环境中的数据
- Runtime那些事儿(消息机制)
- Precision and Recall
- 二维指针和二维数组