leetcode 59. 螺旋矩阵 II

题目描述

代码:

cpp 复制代码
class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> res(n,vector<int>(n,0));
        int num = 1;
        int len = n;
        int start = 0;
        while(len > 0){
            int row = start;
            int column = start;
            if(len==1){
                res[row][column] = num++;
                break;
            }
            start++;
            for(int i =0;i < len-1;i++){
                res[row][column++] = num++;
            }
            for(int i = 0;i < len-1;i++){
                res[row++][column] = num++;
            }
            for(int i = 0;i < len-1;i++){
                res[row][column--] = num++;
            }
            for(int i = 0;i < len-1;i++){
                res[row--][column] = num++;
            }
            len-=2;
        }
        return res;
    }
};
相关推荐
旖旎夜光7 小时前
LeetCode 493: 翻转对 (归并排序) —— 题解
c++·笔记·学习·算法·leetcode·排序算法
木井巳12 小时前
【动态规划: 子序列问题】最长递增子序列的个数
java·算法·leetcode·动态规划·推荐算法
Navigator_Z15 小时前
LeetCode //C - 1326. Minimum Number of Taps to Open to Water a Garden
c语言·算法·leetcode
Rain的Java大神之路1 天前
LeetCode 45 跳跃游戏 II:大厂面试官最爱追问的贪心题,我把骨头给你拆干净了
java·后端·leetcode
Regulus-Moon2 天前
人工智能数学基础:矩阵特征分解与压缩实战
人工智能·线性代数·矩阵
让我们一起加油好吗3 天前
【算法刷题】LeetCode 力扣 Hot 100 刷题记录
数据结构·c++·算法·leetcode·面试·笔试
Rain的Java大神之路3 天前
LeetCode 42「接雨水」:面试官百出不厌的题,一文讲透从暴力到双指针 O(1)
java·后端·leetcode
Navigator_Z5 天前
LeetCode //C - 1320. Minimum Distance to Type a Word Using Two Fingers
c语言·算法·leetcode
wy_hhxx5 天前
[Leetcode] 滑动窗口
算法·leetcode·滑动窗口
AIgorithmGEEK5 天前
算法·哈希表|从“逐个比”到“按 key 算位置”
数据结构·c++·算法·leetcode·hash