Leetcode oj java 54. Spiral Matrix
2016-12-28 12:12
375 查看
一、问题描述:
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:
You should return
二、思路:
画圈圈,思路参考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;
}
}
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;
}
}
相关文章推荐
- ThreadPoolExecutor线程池的使用与理解
- myEclipse budug不起作用的问题
- JDK源码走读(2):容器之PriorityQueue
- iOS and JAVA 的 RSA 加密解密详解
- Java内存结构和数据类型
- java 多线程2
- java多线程
- java发送邮件
- java.lang.SecurityException: class "javax.servlet.FilterRegistration"'s signer information does not
- JDK源码走读(1):开篇
- Struts2 3种动态方法调用方式
- SpringMVC简单使用
- Java+WebSocket实现一对一对多即时聊天
- struts运行原理
- Struts2框架及其设计模式
- Java利用MessageDigest进行MD5加密
- eclipse新建web工程把build改为WebRoot\WEB-INF,WebContent改为WebRoot
- java 总结几种线程异步转同步的方法
- 使用 JUnit 报错 java.lang.Exception: No runnable methods
- 14.玩转Spring Boot 多数据源