leetcode — spiral-matrix-ii
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Source : https://oj.leetcode.com/problems/spiral-matrix-ii/
*
* Created by lverpeng on 2017/7/19.
*
* 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 ]
* ]
*
*/
public class SpiralMatrix2 {
public List<Integer[]> build (int n) {
List<Integer[]> result = new ArrayList<Integer[]>();
for (int i = 0; i < n; i++) {
result.add(new Integer[n]);
}
int row = 0;
int col = 0;
int count = 1;
for (; col < (n+1) / 2 && row < (n+1) / 2; row ++, col++) {
for (int i = col; i < n - col; i++) {
result.get(row)[i] = count++;
}
for (int i = row + 1; i < n - row; i ++) {
result.get(i)[n - col - 1] = count++;
}
for (int i = n - col - 2; i >= col; i--) {
result.get(n - row - 1)[i] = count++;
}
for (int i = n - row - 2; i > row; i--) {
result.get(i)[col] = count++;
}
}
return result;
}
public static void print (List<Integer[]> list) {
for (Integer[] arr : list) {
System.out.println(Arrays.toString(arr));
}
System.out.println();
}
public static void main(String[] args) {
SpiralMatrix2 spiralMatrix2 = new SpiralMatrix2();
print(spiralMatrix2.build(0));
print(spiralMatrix2.build(1));
print(spiralMatrix2.build(2));
print(spiralMatrix2.build(3));
print(spiralMatrix2.build(4));
print(spiralMatrix2.build(5));
}
}
leetcode — spiral-matrix-ii的更多相关文章
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- [LeetCode] Spiral Matrix II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [Leetcode] spiral matrix ii 螺旋矩阵
Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...
- [leetcode]Spiral Matrix II @ Python
原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...
- Leetcode Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- LeetCode Spiral Matrix II (技巧)
题意: 从1开始产生连续的n2个数字,以螺旋的方式填满一个n*n的数组. 思路: 由于是填满一个矩阵,那么只需要每次都填一圈即可.应该注意特殊情况. 迭代: class Solution { publ ...
- Leetcode 54. Spiral Matrix & 59. Spiral Matrix II
54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 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 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
随机推荐
- 每日一练ACM 2019.0418
Problem Description 输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离. Input 输入数据有多组,每组占一行,由4个实数组成,分别表示x1,y1,x2,y2 ...
- Error creating bean with name 'student': Unsatisfied dependency expressed through field 'teacher'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating
有 有参构造但是没有无参构造...
- BJOI2018 简要题解
二进制 序列上线段树维护DDP好题. 题解可以看这篇 代码: #include<bits/stdc++.h> #define ri register int using namespace ...
- 中标麒麟(linux)下Qt调用python数据转换
转自:https://blog.csdn.net/itas109/article/details/78733478 mytest.py文件 # -*- coding: utf-8 -*- def he ...
- Normalize.css & Reset
Normalize.css: try to keep the style consistent in every browser. Reset: clear style in every browse ...
- 自我理解node.js相比java的优势
今天学习node.js,相比于之前学习过的java,node.js有一些优越之处.原因是它是一个基于Chrome v8引擎建立的JavaScript运行平台. (1)创建服务器:自行服务器来监听客户端 ...
- vue.js 2.0(2)
1.双向绑定v-model要写在输入框里 2.点击改变颜色:当index和isActive统一时,才会调用class html: function: css: 3.在同一 ...
- DOS 命令 os系统(windows)
一.cd 相关操作 1."cd .. "or "cd ..\" --返回上一级 2.cd E:\Python -- 进入目录 二.dir --drectory ...
- Windows 10 IoT Core 17101 for Insider 版本更新
除夕夜,微软发布了Windows 10 IoT Core 17101 for Insider 版本更新,本次更新只修正了一些Bug,没有发布新的特性. 已知的问题: F5 driver deploym ...
- [转] 如何用kaldi训练好的模型做特定任务的在线识别
转自:http://blog.csdn.net/inger_h/article/details/52789339 在已经训练好模型的情况下,需要针对一个新任务做在线识别应该怎么做呢? 一种情况是,用已 ...