力扣.旋转矩阵Ⅱ

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;
        
    }
};

欢迎各位读者提出意见。

(菜菜奋斗小日记)

相关推荐
nlpming3 分钟前
opencode MCP(Model Context Protocol)配置手册
算法
MATLAB代码顾问16 分钟前
MATLAB实现灰狼算法优化PID参数
算法·机器学习·matlab
承渊政道1 小时前
【动态规划算法】(完全背包问题从状态定义到空间优化)
数据结构·c++·学习·算法·leetcode·动态规划·哈希算法
超级大福宝1 小时前
【力扣48. 旋转图像】超好记忆版 + 口诀
c++·算法·leetcode
爱写代码的倒霉蛋1 小时前
2023年天梯赛L1-8
数据结构·算法
apollowing2 小时前
启发式算法WebApp实验室:从搜索策略到群体智能的能力进阶(上)
算法·启发式算法·web app
生物信息与育种2 小时前
黄三文院士领衔植物星球计划(PLANeT)发表Cell
人工智能·深度学习·算法·面试·transformer
aini_lovee2 小时前
WSN 四大经典无需测距定位算法
算法
人道领域2 小时前
【LeetCode刷题日记】掌握二叉树遍历:栈实现的三种绝妙方法
算法·leetcode·职场和发展