leetcode59 spiral matrix2
思路与54相同,由于为行=列的数组,再判断何时跳出时可简化步骤。
class Solution {
public int[][] generateMatrix(int n) {
int[][] matrix = new int[n][n];
int u=0,d=n-1,s=1;
while(d>=u){
for(int i=u;i<=d;i++) {matrix[u][i]=s++;}u++;
for(int i=u;i<=d;i++) {matrix[i][d]=s++;}//r--;
for(int i=d-1;i>=u-1;i--) {matrix[d][i]=s++;}d--;
for(int i=d;i>=u;i--) {matrix[i][u-1]=s++;}//l++;
}
return matrix;
}
}
leetcode59 spiral matrix2相关推荐
- LeetCode59 Spiral Matrix II
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
- LeetCode github集合,附CMU大神整理笔记
Github LeetCode集合 本人所有做过的题目都写在一个java项目中,同步到github中了,算是见证自己的进步.github目前同步的题目是2020-09-17日之后写的题.之前写过的题会 ...
- LeetCode:Spiral Matrix I II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- LeetCode 54. Spiral Matrix--Python解法--螺旋排序
题目地址:Spiral Matrix - LeetCode Given a matrix of m x n elements (m rows, n columns), return all eleme ...
- Leetcode: Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 旋转遍历矩阵 Spiral Matrix
为什么80%的码农都做不了架构师?>>> 问题: Given a matrix of m x n elements (m rows, n columns), return al ...
- LeetCode 59 Spiral Matrix II(螺旋矩阵II)(Array)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5214 ...
- LeetCode Spiral Matrix II (生成螺旋矩阵)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. F ...
- 59. Spiral Matrix II
/** 59. Spiral Matrix II * 12.5 by Mingyang* 注意,这里我们说的Matrix就是正方形,不再是长方形了,所以我们会用* 更简单的方法,就是直接上下左右分别加 ...
最新文章
- Python的知识点 plt.plot()函数细节
- span 可编辑 控制数量_CSS如何限制显示的文本字数
- ios中的自动释放池
- 全球厂商已向自动驾驶投入800亿美元 依然群龙无首 | 厚势
- 第二阶段冲刺第十天,6月9日。
- Ubuntu+PHP+Apache+Xdebug 使用方法
- el表达式中换行_angularjs/ng-bind如何保留换行符
- 解决: service endpoint with name xxx already exists ( docker 已删除的容器却依旧存在)
- ASP.NET页面与IIS底层交互和工作原理详解(一)
- java 打印制表符_开发简单的Java应用
- VMware Fusion for Apple silicon
- HC05蓝牙模块与stm32通信
- 对于SOAP协议的全面介绍
- 手把手教你快速搭建私服环境
- 线扫相机DALSA-Image Format(读图格式设置)
- python-课后作业-4
- 《拥抱机器人时代——Servo杂志中文精华合集》——4.5 审视无线技术
- k-d树(Kd trees)
- java8通过 poi+text 将word转为pdf
- 计算机算法讲解的ppt,算法讲解ppt课件.ppt