博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spiral Matrix II
阅读量:4074 次
发布时间:2019-05-25

本文共 664 字,大约阅读时间需要 2 分钟。

Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.

For example,
Given n = 3,

You should return the following matrix:
[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]
Java代码:

public class Solution {   public int[][] generateMatrix(int n) {    int[][] ans = new int[n][n];    int curr = 1;    for (int i = 0, j = 0; i<(n+1)/2 ; ++i, ++j){        for (int k = j; k
=n/2) break; // this is needed for odd n's. for (int k = i+1; k
j; --k){ ans[n-1-i][k] = curr++; } for (int k = n-1-i; k>i; --k){ ans[k][j] = curr++; } } return ans;}}
 

转载地址:http://biuni.baihongyu.com/

你可能感兴趣的文章
eureka-client.properties文件配置
查看>>
MODULE_DEVICE_TABLE的理解
查看>>
platform_device与platform_driver
查看>>
platform_driver平台驱动注册和注销过程(下)
查看>>
.net强制退出主窗口的方法——Application.Exit()方法和Environment.Exit(0)方法
查看>>
c# 如何调用win8自带的屏幕键盘(非osk.exe)
查看>>
build/envsetup.sh 简介
查看>>
Android framework中修改或者添加资源无变化或编译不通过问题详解
查看>>
linux怎么切换到root里面?
查看>>
linux串口操作及设置详解
查看>>
安装alien,DEB与RPM互换
查看>>
编译Android4.0源码时常见错误及解决办法
查看>>
Android 源码编译make的错误处理
查看>>
linux环境下C语言中sleep的问题
查看>>
ubuntu 12.04 安装 GMA3650驱动
查看>>
新版本的linux如何生成xorg.conf
查看>>
xorg.conf的编写
查看>>
启用SELinux时遇到的问题
查看>>
virbr0 虚拟网卡卸载方法
查看>>
No devices detected. Fatal server error: no screens found
查看>>