力扣.旋转矩阵Ⅱ

59. 螺旋矩阵 II - 力扣(LeetCode)

代码区:

cpp 复制代码
class Solution {
    const int MAX=25;
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> ans;
        vector<int> hang;
        int len_n=n;
        int arry[25][25];
        int start_hang=0,start_lie=0,end_hang=len_n-1,end_lie=len_n-1;
        int step=1;
        while(step<=n*n){
            for(int i=start_lie;i<=end_lie;i++){//行不变列增加
                arry[start_hang][i]=step;
                step++;
            }
            start_hang++;
            for(int i=start_hang;i<=end_hang;i++ ){//列不变行增加
                arry[i][end_lie]=step;
                step++;
            }
            end_lie--;
            for(int i=end_lie;i>=start_lie;i--){//行不变,列减小
                arry[end_hang][i]=step;
                step++;
            }
            end_hang--;
            for(int i=end_hang;i>=start_hang;i-- ){//列不变,行减小
                arry[i][start_lie]=step;
                step++;
            }
            start_lie++;
        }
        for(int i=0;i<len_n;i++){
            for(int j=0;j<len_n;j++){
                hang.push_back(arry[i][j]);
            }
            ans.push_back(hang);
            hang.clear();
        }
        return ans;
        
    }
};

欢迎各位读者提出意见。

(菜菜奋斗小日记)

相关推荐
罗西的思考6 小时前
DreamZero 与 DreamDojo:世界模型与策略的分层协同综合分析与对比
人工智能·算法·机器学习
玖玥拾6 小时前
LeetCode 219 存在重复元素 II
算法·leetcode·哈希算法·散列表
知无不研7 小时前
c语言中循环的介绍与简单应用
c语言·开发语言·算法·循环·for·while
a187927218318 小时前
【算法】动态规划第四篇:背包收官——min 哨兵、计数世界与组合排列分水岭
算法·leetcode·动态规划·dp·01背包·算法讲解·决策合并
2601_962297489 小时前
在python3中、下列输出变量a的正确写法是_2020超星大数据Python免费答案
数据结构·python·算法·编程·字符串操作
辰烨chenye9 小时前
LeetCode Hot 100 题解 · 子串篇
算法·leetcode·职场和发展
辰烨chenye9 小时前
LeetCode Hot 100 题解 · 堆篇
java·算法·leetcode
手写码匠9 小时前
华为云Flexus+DeepSeek征文|DeepSeek 应用监控告警体系实战:从“用户投诉才知道“到“故障前就发现“
人工智能·深度学习·算法·aigc
shehuiyuelaiyuehao10 小时前
算法29,前缀和,除自身以外的数组的乘积
java·数据结构·算法
智购科技自动售货机厂家10 小时前
2026自动售货机设备清洁效果自动验证:从图像比对到评分算法的工程实践~YH
人工智能·算法·计算机视觉