[4]54. Spiral Matrix/59. Spiral Matrix II(Java)
来源:互联网 发布:阿里云cdn加速如何使用 编辑:程序博客网 时间:2024/06/05 01:49
54. Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
For example,
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].
class Solution { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> res = new ArrayList<>(); if (matrix.length == 0) return res; int rowBegin = 0, colBegin = 0; int rowEnd = matrix.length - 1; int colEnd = matrix[0].length - 1; while (rowBegin <= rowEnd && colBegin <= colEnd) { // Traverse right for (int j = colBegin; j <= colEnd; j ++) { res.add(matrix[rowBegin][j]); } rowBegin ++; // Traverse down for (int i = rowBegin; i <= rowEnd; i ++) { res.add(matrix[i][colEnd]); } colEnd --; if (rowBegin <= rowEnd) { // Traverse left for (int j = colEnd; j >= colBegin; j --) { res.add(matrix[rowEnd][j]); } rowEnd --; } if (colBegin <= colEnd) { // Traverse up for (int i = rowEnd; i >= rowBegin; i --) { res.add(matrix[i][colBegin]); } colBegin ++; } } return res; }}
59. Spiral Matrix II
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 ]]
class Solution { public int[][] generateMatrix(int n) { int[][] matrix = new int[n][n]; if (n == 0) return matrix; int rowBegin = 0, colBegin = 0; int rowEnd = n - 1, colEnd = n - 1; int num = 1; while (rowBegin <= rowEnd && colBegin <= colEnd) { // Traverse right for (int j = colBegin; j <= colEnd; j ++) { matrix[rowBegin][j] = num ++; } rowBegin ++; // Traverse down for (int i = rowBegin; i <= rowEnd; i ++) { matrix[i][colEnd] = num ++; } colEnd --; if (rowBegin <= rowEnd) { // Traverse left for (int j = colEnd; j >= colBegin; j --) { matrix[rowEnd][j] = num ++; } rowEnd --; } if (colBegin <= colEnd) { // Traverse up for (int i = rowEnd; i >= rowBegin; i --) { matrix[i][colBegin] = num ++; } colBegin ++; } } return matrix; }}
阅读全文
0 0
- [4]54. Spiral Matrix/59. Spiral Matrix II(Java)
- 54. Spiral Matrix && 59. Spiral Matrix II
- 59. Spiral Matrix II && 54. Spiral Matrix
- 【LeetCode-Java】54. Spiral Matrix+59. Spiral Matrix II
- Spiral Matrix II (Java)
- 59.Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 数组和链表的区别
- String & Date
- mybatis入门基础(三)----SqlMapConfig.xml全局配置文件解析
- [LeetCode][2 Add Two Numbers][medium]Java实现
- HDFS文件系统和元数据合并流程以及namenode启动流程
- [4]54. Spiral Matrix/59. Spiral Matrix II(Java)
- 使用Lamdba 表达式 从A集合中去除不存在于B集合的元素加入到C集合中(快速找出两个集合中不同的元素)
- 通过反射完成不同对象之间的属性拷贝
- 在mybatis报错Open quote is expected for attribute "property" associated with an element type "result".
- mybatis入门基础(四)----输入映射和输出映射
- 程序源的情感生活
- postgres创建用户,修改用户密码,创建数据库
- mybatis入门基础(五)----动态SQL
- Lecture 7_2: Lists and mutability, dictionaries, pseudocode, introduction to efficiency