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 ...
随机推荐
- java 面试随笔
---恢复内容开始--- 1.自我介绍 2.你在项目开发过程中遇到的那些问题! 3.懂bootstrap么?简单介绍一下 4.spring的会话数据是怎样的. 5.为什么会有session 因为htt ...
- Pythonの坑
Python closures and late binding A closure occurs when a function has access to a local variable fro ...
- 【刷题】BZOJ 3529 [Sdoi2014]数表
Description 有一张n×m的数表,其第i行第j列(1<=i<=n,1<=j<=m)的数值为能同时整除i和j的所有自然数之和.给定a,计算数表中不大于a的数之和. In ...
- BZOJ 2844: albus就是要第一个出场
2844: albus就是要第一个出场 Time Limit: 6 Sec Memory Limit: 128 MBSubmit: 1134 Solved: 481[Submit][Status] ...
- 使用Java连接HBASE过程中问题总结
本文是我个人在连接服务器的HBASE过程的一些问题总结. 一.用户和主机名的设置 1.1 报错:Insufficient permissions(user=Administartor) 原因:本地ho ...
- hdu6057 Kanade's convolution 【FWT】
题目链接 hdu6057 题意 给出序列\(A[0...2^{m} - 1]\)和\(B[0...2^{m} - 1]\),求所有 \[C[k] = \sum\limits_{i \; and \; ...
- 一次lvs迁移记录
需求:从117.119.33.99迁移到122.14.206.125,lvs为dr模式,系统版本为debian7 1.安装lvs和keepalived # aptitude install -y ip ...
- Hbase(七)hbase高级编程
一.Hbase结合mapreduce 为什么需要用 mapreduce 去访问 hbase 的数据? ——加快分析速度和扩展分析能力 Mapreduce 访问 hbase 数据作分析一 ...
- Python实现类似JavaScript 的Json对象
Python实现类似JavaScript 的Json对象 用过js的都知道 js中json也是一个对象,所以可以直接通过class.attr 取值,当attr不存在时也不会报错,那么Python可不可 ...
- python的map函数的使用方法详解以及使用案例(处理每个元素的自增、自减、平方等)
1.用我们之前学过的求一下平方(只有一个列表) #求平方 num=[1,5,6,2,7,8] a=[] for n in num: a.append(n**2) print (a) C:\python ...