Spiral Matrix I & II
Spiral Matrix I
Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order.
Given n = 3
,
You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
分析:
从上,右,下,左打印。
public class Solution {
public int[][] generateMatrix(int n) { int[][] arr = new int[n][n];
int a = ;
int b = n - ;
int k = ; while (a < b) {
for (int i = a; i <= b; i++) {
arr[a][i] = k++;
} for (int i = a + ; i <= b - ; i++) {
arr[i][b] = k++;
} for (int i = b ; i >= a; i--) {
arr[b][i] = k++;
} for (int i = b - ; i >= a + ; i--) {
arr[i][a] = k++;
} a++;
b--;
}
// if n is odd, it will be executed, if it is even, it won't be executed.
if (a == b) {
arr[a][b] = k;
}
return arr;
}
}
Spiral Matrix II
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
Given the following matrix:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5]
.
分析:
拿到左上角和右下角的坐标,然后从上,右,下,左打印。然后更新坐标。
public class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> list = new ArrayList<>(); if (matrix == null || matrix.length == || matrix[].length == ) return list;
int a = , b = ;
int x = matrix.length - , y = matrix[].length - ; while (a <= x && b <= y) {
// top row
for (int i = b; i <= y; i++) {
list.add(matrix[a][i]);
}
// right column
for (int i = a + ; i <= x - ; i++) {
list.add(matrix[i][y]);
}
// bottom row
if (a != x) {
for (int i = y; i >= b; i--) {
list.add(matrix[x][i]);
}
}
// left column
if (b != y) {
for (int i = x - ; i >= a + ; i--) {
list.add(matrix[i][b]);
}
} a++;
b++;
x--;
y--;
}
return list;
}
}
Spiral Matrix I & II的更多相关文章
- 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 ...
- Spiral Matrix I&&II
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 59. Spiral Matrix && Spiral Matrix II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- 【leetcode】59.Spiral Matrix II
Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...
- 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 ...
随机推荐
- centOS基本操作和命令(更新)
1.文字输入和图形界面切换 CTRL+ALT+(F1~F6)为切换至文字输入,分别对应六个不同输入界面,可用以不同账号:CTRL+ALT+F7为切换至图形界面 2.修改时区 date -R date ...
- BZOJ5322 JXOI2018排序问题
对于一个序列,重排后有序的概率显然是∏cnti!/n!,其中cnti为第i种数出现次数.要使概率最小,显然应该让各种数字尽量平均分配.剩下的是div2BC左右的大讨论. #include<ios ...
- 【刷题】BZOJ 4816 [Sdoi2017]数字表格
Description Doris刚刚学习了fibonacci数列.用f[i]表示数列的第i项,那么 f[0]=0 f[1]=1 f[n]=f[n-1]+f[n-2],n>=2 Doris用老师 ...
- 一步步创建第一个Docker App —— 1. 背景介绍
原文:https://docs.docker.com/engine/getstarted-voting-app/#/docker-stacks-and-services 你将会学习什么 本文创建 ...
- java类加载详解
1,类的加载过程: JVM将类加载过程分为三个步骤:装载(load),链接(link)和初始化(initialize),其中链接又分为三个步骤: 验证(varification),准备(Prepara ...
- 解题:WC 2007 石头剪刀布
题面 要我们把边定向,最大化留下来的三元环数目......并不能直接做,考虑容斥,去掉不合法的数目. 那么三个点不成环当且仅当有一个点出度为2一个点入度为2,发现最终答案就是$C_n^3-\sum C ...
- 【DSU on tree】【CF741D】Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths
Description 给定一棵 \(n\) 个节点的树,每条边上有一个字符,字符集大小 \(22\),求每个节点的子树内最长的简单路径使得路径上的字符经过重排后构成回文串. Limitation \ ...
- Centos Python3安装共存
安装python3.6可能使用的依赖 yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlit ...
- supervisor 添加新配置不生效的问题
supervisorctl reread supervisorctl reload (不运行这一步会导致启动不了) supervisorctl start xxx:* 提示:No config upd ...
- 【题解】Tree-String Problem Codeforces 291E AC自动机
Prelude 传送到Codeforces:(/ω\)--- (/ω•\) Solution 很水的一道题. 对查询的串建出来AC自动机,然后树上随便跑跑就行了. 为什么要写这篇题解呢? 我第一眼看到 ...