Leetcode oj java 54. Spiral Matrix
来源:互联网 发布:golang http 断点续传 编辑:程序博客网 时间:2024/04/29 14:31
一、问题描述:
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]
.
二、思路:
画圈圈,思路参考http://blog.csdn.net/u011060119/article/details/53906780
但是要注意到只有单独一行或者单独一列的情况。
三、代码:
package T12;import java.util.ArrayList;import java.util.List;/** * @author 作者 : xcy * @version 创建时间:2016年12月28日 上午11:21:12 * 类说明 */public class t54 { public static void main(String[] args) { // TODO Auto-generated method stub int[][] matrix = { { 1, 2, 3, 4, 5 } }; List<Integer> re = spiralOrder(matrix); for (int i = 0; i < re.size(); i++) { System.out.println(re.get(i)); } } public static List<Integer> spiralOrder(int[][] matrix) { List<Integer> re = new ArrayList<Integer>(); int m = matrix.length; if (m < 1) { return re; } int n = matrix[0].length; int start = 0; while (m > start * 2 && n > start * 2) { // add re = addNum(re, matrix, m, n, start); start++; } return re; } public static List<Integer> addNum(List<Integer> list, int[][] matrix, int m, int n, int start) { int i = start; int j = start; boolean flag = n % 2 == 1 ? true : false; // 从左到右 for (j = start; j < (n - start); j++) { list.add(matrix[i][j]); } // 从上到下 j = n - start - 1; for (i = start + 1; i < (m - start); i++) { list.add(matrix[i][j]); } //从右到左 i = m - start - 1; if (i != start) { for (j = n - start - 2; j >= start; j--) { list.add(matrix[i][j]); } } // 从下到上 if (flag && start == n / 2) { return list; } j = start; for (i = m - start - 2; i > start; i--) { list.add(matrix[i][j]); } return list; }}
0 0
- Leetcode oj java 54. Spiral Matrix
- LeetCode OJ:Spiral Matrix
- LeetCode OJ - Spiral Matrix
- LeetCode OJ Spiral Matrix
- leetcode oj java 59. Spiral Matrix II
- leetcode-java-54. Spiral Matrix
- (Java)LeetCode-54. Spiral Matrix
- [leetcode]54. Spiral Matrix(Java)
- 【LeetCode-Java】54. Spiral Matrix+59. Spiral Matrix II
- LeetCode OJ:Spiral Matrix II
- LeetCode OJ - Spiral Matrix II
- [Leetcode] Spiral Matrix (Java)
- [LeetCode][Java] Spiral Matrix
- leetcode:Spiral Matrix 【Java】
- Leetcode 54. Spiral Matrix (Medium) (java)
- LeetCode 54. Spiral Matrix
- [LeetCode]54.Spiral Matrix
- LeetCode --- 54. Spiral Matrix
- drools-kie API 和kmodule.xml 文件详解
- 《年轻时,你最该拒绝什么?-王锋》
- Android Studio自动生成选择器插件SelectorChapek
- https 配置指南
- ThreadPoolExecutor线程池的使用与理解
- Leetcode oj java 54. Spiral Matrix
- 复制文件显示进度
- android webview简单加载echart表格(折线,饼形,柱形)
- python中的多线程和多进程
- A1036. Boys vs Girls (25)
- 使用AIDL方式启动Service 实现跨进程通信
- apache ssl
- 本地文件的批量创建,修改工具类
- android异步的几种方式