[LeetCode] 59. Spiral Matrix II
来源:互联网 发布:mac gradle 安装 编辑:程序博客网 时间:2024/06/03 19:18
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n = 3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]
class Solution {public: vector<vector<int>> generateMatrix(int n) { vector<vector<int>> matrix(n, vector<int>(n)); generateMatrix(matrix, 0, n, 1); return matrix; }private: void generateMatrix(vector<vector<int>> &matrix, int startpoint, int n, int baseval) { if (n <= 0) return; else if (n == 1) { matrix[startpoint][startpoint] = baseval; return; } else { for (int j = startpoint; j < startpoint + n - 1; j++) matrix[startpoint][j] = baseval++; for (int i = startpoint; i < startpoint + n - 1; i++) matrix[i][startpoint + n - 1] = baseval++; for (int j = startpoint + n - 1; j > startpoint; j--) matrix[startpoint + n - 1][j] = baseval++; for (int i = startpoint + n - 1; i > startpoint; i--) matrix[i][startpoint] = baseval++; generateMatrix(matrix, startpoint + 1, n - 2, baseval); } }};
阅读全文
0 0
- [LeetCode]59.Spiral Matrix II
- LeetCode --- 59. Spiral Matrix II
- [Leetcode] 59. Spiral Matrix II
- [leetcode] 59.Spiral Matrix II
- LeetCode 59. Spiral Matrix II
- 59. Spiral Matrix II LeetCode
- [leetcode] 59. Spiral Matrix II
- leetcode 59. Spiral Matrix II
- LeetCode 59. Spiral Matrix II
- leetcode 59. Spiral Matrix II
- LeetCode *** 59. Spiral Matrix II
- leetcode 59. Spiral Matrix II
- Leetcode 59. Spiral Matrix II
- LeetCode 59. Spiral Matrix II
- LeetCode - 59. Spiral Matrix II
- [LeetCode] 59. Spiral Matrix II
- [leetcode] 59. Spiral Matrix II
- [leetcode]59. Spiral Matrix II
- 自由泳如何正确换气?掌握4大关键帮你摆脱困惑
- 大白话讲解Promise(二)理解Promise规范
- 开发环境搭建中的网络代理问题
- 尚未佩妥剑,转眼继续佩
- 全套java教程
- [LeetCode] 59. Spiral Matrix II
- for循环
- Sqoop1.4.6使用数据导入导出
- Android Activity ABC页面跳转生命周期变化及activity + fragment生命周期变化(经典面试题)
- android-获取设备id
- 交换机应用之端口模式(access、trunk和hybird)、是否标记(tag、untag)、端口缺省vlan(pvid、native id)
- 将不在pom文件中的jar包打包WEB-INF/lib目录下
- 机器学习中常用的Numpy函数
- 7月16日|C语言|volatile用法及static用法及printf返回值研究