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;
    }
};
相关推荐
薰衣草233319 小时前
滑动窗口(2)——不定长
python·算法·leetcode
YuTaoShao1 天前
【LeetCode 每日一题】1277. 统计全为 1 的正方形子矩阵
算法·leetcode·矩阵
野犬寒鸦1 天前
力扣hot100:相交链表与反转链表详细思路讲解(160,206)
java·数据结构·后端·算法·leetcode
阿昭L1 天前
leetcode两数之和
算法·leetcode
Lris-KK1 天前
【Leetcode】高频SQL基础题--1164.指定日期的产品价格
sql·leetcode
Swift社区1 天前
Swift 解法详解:LeetCode 371《两整数之和》
开发语言·leetcode·swift
Swift社区1 天前
Swift 解法详解 LeetCode 362:敲击计数器,让数据统计更高效
开发语言·leetcode·swift
小刘的AI小站2 天前
leetcode hot100 二叉搜索树
算法·leetcode
自信的小螺丝钉2 天前
Leetcode 876. 链表的中间结点 快慢指针
算法·leetcode·链表·指针
红豆怪怪2 天前
[LeetCode 热题 100] 32. 最长有效括号
数据结构·python·算法·leetcode·动态规划·代理模式