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 ...
随机推荐
- uni-app 点击切换图标
前端代码: <template> <view> <image src="../../static/pari/buxihuan.png" v-show= ...
- MyBatis generator配置 overwrite 文件覆盖失效
工具:IDEA.jdk1.8.mysql 底部有解决方法! pom.xml配置 <plugins> <!--Mybatis自动代码插入--> <plugin> &l ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- 【收藏】JS获取鼠标的X,Y坐标位置
JS的方法: <html> <head> <meta http-equiv="Content-Type" content="text/htm ...
- App性能测试之启动时间(安卓)手动+脚本
这个测试可以使用adb工具,adb的安装方式 测试策略 安装后首次启动 常规冷启动 热启动(一般这个都很少测试) 针对1和2的测试方法 步骤1:在cmd中输入如下命令 adb logcat * > ...
- maven项目打包成war包发布到tomcat下...
分为两种情况: 1.当你的项目中没有前端页面时,直接右键项目--Export--选择web下的warFile--选择目录到tomcat安装目录下的webapps下即可访问. 2.当项目中有前端页面时 ...
- 机器学习常用sklearn库
Sklearn.model_selection(模型选择) Cross_val_score:交叉验证 Train_test_split:数据切割 GridsearchCV:网格搜索 Sklearn.m ...
- Navicat连接oracle库报错的处理方法
1.oracle客户端版本:11.2.0 64X navicat premium客户端:12.0. 64X 2.安装后,打开连接,输入连接信息,点击保存,提示oracle library is no ...
- 最小化安装的centos7.5上编译安装git2.19
VMware Workstation已经采用最小化安装CentOS7,显示版本为CentOS7.5,准备采用yum安装git. 采用yum list git发现可安装的GIT软件包版本1.8.3.1, ...
- Android-Java-接口Interface
接口Interface 与 抽象类不同: 抽象类关注的是事物本质,例如:水果Fruit 属于抽象的,说去买水果 是模糊的概念 是抽象的概念 不具体,到底买什么水果不知道,而水果包含了 香蕉,橘子 很多 ...