产生一个螺旋矩阵
来源:互联网 发布:阿里云ecs搭建ss 编辑:程序博客网 时间:2024/05/20 01:10
public class Solution { public int[][] generateMatrix(int n) { if (n == 0) return new int[0][0]; int[][] matrix = new int[n][n]; int up = 0, down = matrix.length - 1; int total = 1; while (up < down) { total = genetare(matrix, up, down, total); up++; down--; } if (up == down) matrix[up][up] = total; return matrix; } public int genetare (int[][] matrix, int up, int down, int total) { for (int i = up; i <= down; i++) matrix[up][i] = total++; total--; for (int i = up; i <= down; i++) matrix[i][down] = total++; total--; for (int i = down; i >= up ; i--) matrix[down][i] = total++; total--; for (int i = down; i > up ; i--) matrix[i][up] = total++; return total; } }想法很简单,就是看对于边界条件的判断。
0 0
- 产生一个螺旋矩阵
- 一个螺旋矩阵的类
- 一个螺旋矩阵的类
- 螺旋矩阵
- 螺旋矩阵
- 螺旋矩阵
- 螺旋矩阵
- 螺旋矩阵
- 螺旋矩阵!
- 螺旋矩阵
- 螺旋矩阵
- 螺旋矩阵
- 螺旋矩阵:)
- 螺旋矩阵
- 螺旋矩阵
- 螺旋矩阵
- 螺旋矩阵
- 螺旋矩阵
- 第四周项目(4):建设双链表算法库
- AI设计---Robot(v1.0),简单对话的实现
- 查询在2月份入职的所有员工信息
- uvalive 3485 - 数值积分
- HDU 2547 无剑无我(水~)
- 产生一个螺旋矩阵
- 系统程序员成长计划读后感
- const成员函数、inline成员函数、static成员函数中关键字的位置
- postMessage安全性问题
- java篇 【14】Java 继承
- 关于应急道占用问题
- 【Cpp】删除字符串空格
- 安卓控件使用系列20:ProgressBar进度条控件的使用方法
- 弱校联萌十一大决战之强力热身 C题