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 ...
随机推荐
- linux eclipse add desktop shortcut with root permission
gedit ~/.local/share/applications/opt_eclipse.desktop sudo apt-get install gksu [Desktop Entry] Type ...
- es各类SearchType的意思
元素 含义 QUERY_THEN_FETCH 查询是针对所有的块执行的,但返回的是足够的信息,而不是文档内容(Document).结果会被排序和分级,基于此,只有相关的块的文档对象会被返回.由于被取到 ...
- MT【122】一个重要的不平凡的无穷级数
求证:$1+\dfrac{1}{4}+\dfrac{1}{9}+\cdots +\dfrac{1}{n^2}+\cdots = \dfrac{\pi^2}6$. 解答:考虑$$\dfrac{\sin ...
- AtCoder Grand Contest 005
AtCoder Grand Contest 005 A - STring 翻译 给定一个只包含\(ST\)的字符串,如果出现了连续的\(ST\),就把他删去,然后所有位置前移.问最后剩下的串长. 题解 ...
- Miiler-Robin素数测试与Pollard-Rho大数分解法
板题 Miiler-Robin素数测试 目前已知分解质因数以及检测质数确定性方法就只能\(sqrt{n}\)试除 但是我们可以基于大量测试的随机算法而有大把握说明一个数是质数 Miler-Robin素 ...
- 【codeforces 235E】 Number Challenge
http://codeforces.com/problemset/problem/235/E (题目链接) 题意 给出${a,b,c}$,求${\sum_{i=1}^a\sum_{j=1}^b\sum ...
- 移动端图片轮播—swipe滑动插件
swipe是一个轻量级的移动滑动组件,它可以支持精确的触滑移动操作,能解决移动端对滑动的需求. swipe插件的使用主要有四大块: 一.html <div id='slider' class=' ...
- 解题:洛谷4721 [模板]分治FFT
题面 这是CDQ入门题,不要被题目名骗了,这核心根本不在不在FFT上啊=.= 因为后面的项的计算依赖于前面的项,不能直接FFT.所以用CDQ的思想,算出前面然后考虑给后面的贡献 #include< ...
- bzoj 3779: 重组病毒
一道好题~~ 一个点到根传染需要的时间是这段路径上不同颜色的数目,一个点子树到根平均传染时间就是加权平均数了(好像是废话). 所以只要用线段树维护dfs序就这个可以了,换根的话一个点的子树要么在dfs ...
- Chapter 4(栈与队列)
1.栈的顺序存储结构 //*********************************stack_array.h************************************ #ifn ...