好贷网好贷款

Spiral Matrix

发布时间:2016-12-3 6:20:16 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"Spiral Matrix",主要涉及到Spiral Matrix方面的内容,对于Spiral Matrix感兴趣的同学可以参考一下。

public ArrayList<Integer> spiralOrder(int[][] matrix) { // Start typing your Java solution below // DO NOT write main() function ArrayList<Integer> result = new ArrayList<Integer>(); int m = matrix.length; if(m == 0) return result; int n = matrix[0].length; return spiralOrder(matrix, 0, 0, m, n); } public ArrayList<Integer> spiralOrder(int[][] matrix, int row, int column, int rLength, int cLength) { ArrayList<Integer> result = new ArrayList<Integer>(); if(rLength <= 0 || cLength <= 0) return result; if(rLength == 1) { for(int i = column; i < cLength + column; i++) { result.add(matrix[row][i]); } return result; }else if(cLength == 1) { for(int i = row; i < rLength + row; i++) { result.add(matrix[i][column]); } return result; }else{ for(int i = column; i < cLength + column - 1; i++) { result.add(matrix[row][i]); } for(int i = row; i < rLength + row - 1; i++) { result.add(matrix[i][cLength + column - 1]); } for(int i = column; i < cLength + column - 1; i++) { result.add(matrix[rLength + row - 1][cLength + 2 * column - 1 - i]); } for(int i = row; i < rLength + row - 1; i++) { result.add(matrix[rLength + 2 * row - 1 - i][column]); } } result.addAll(spiralOrder(matrix, row + 1, column + 1, rLength - 2, cLength - 2)); return result; }

上一篇:Linux入门基础 #11:获取帮助
下一篇:职场新闻:高薪抢”菜鸟”(应届生) 阿里疯了吗?

相关文章

关键词: Spiral Matrix

相关评论