题目一:

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].

解答:

采用从最外层一层一层向内操作的方法。

public class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<>();
if(matrix==null||matrix.length==0||matrix[0].length==0) return res;
//每一圈左上角数字的坐标为(top,left),右下角的数字的坐标为(bottom,right)
int top = 0, left = 0;
int bottom = matrix.length - 1, right = matrix[0].length - 1;
while (top <= bottom && left <= right) {
outEdge(res, matrix, left++, top++, right--, bottom--);
}
return res;
}
private static void outEdge(List<Integer> res, int[][] matrix, int left, int top, int right, int bottom) {
if (top == bottom) {
for (int i = left; i <= right; i++) {
res.add(matrix[top][i]);
}
} else if (left == right) {
for (int i = top; i <= bottom; i++) {
res.add(matrix[i][left]);
}
} else {
int curRow = top;
int curCol = left;
while (curCol < right) {
res.add(matrix[top][curCol++]);
}
while (curRow < bottom) {
res.add(matrix[curRow++][right]);
}
while (curCol > left) {
res.add(matrix[bottom][curCol--]);
}
while (curRow > top) {
res.add(matrix[curRow--][left]);
}
}
}
}

题目二:

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 ]
]
public static int[][] generateMatrix(int n) {
int[][] res = new int[n][n];
int top = 0, left = 0;
int bottom = n - 1, right = n - 1;
int num = 1;
while (left<=right&&top<=bottom) {
num= inputEdge(num, res, left++, top++, right--, bottom--);
}
return res;
}
private static int inputEdge(int num, int[][] res, int left, int top, int right, int bottom) {
int curRow = top;
int curCol = left;
if (top==bottom&&left==right){
res[top][left]=num;
return num;
} while (curCol < right) {
res[top][curCol++] = num++;
}
while (curRow < bottom) {
res[curRow++][right] = num++;
}
while (curCol > left) {
res[bottom][curCol--] = num++;
}
while (curRow > top) {
res[curRow--][left] = num++;
}
return num;
}

最新文章

  1. eclipse快捷键以及使用技巧大全
  2. 防止apache下面直接输入目录访问文件
  3. python-unexpected content storage modification出错
  4. SRM 442(1-250pt, 1-500pt)
  5. javascript——处理(获取)浏览器版本、操作系统
  6. Qt install Phonon
  7. 定制一个winCE5.0操作系统
  8. python实现TCP/UDP通信
  9. 超绚丽CSS3多色彩发光立方体旋转动画
  10. Nginx与ftp服务器
  11. 在Web根目录下建立testdb.php文件内容
  12. Redis和RabbitMQ在项目中的使用
  13. 浅析ARM公司在物联网领域的战略布局
  14. [Android] 修图工具Draw9patch使用小结(附ubuntu快捷截图方法)
  15. delphi 实现文件上传下载
  16. Flash Player离线安装包下载指南
  17. GitLab项目迁移到Gerrit
  18. 【LeetCode】 617. 合并二叉树
  19. JSP/Servlet中文乱码处理总结
  20. 申请单位iOS开发者账号

热门文章

  1. script 标签 幼儿园级别的神坑。居然还让我踩到了。
  2. Google Code Jam 2014 资格赛:Problem D. Deceitful War
  3. ubuntu安装源
  4. saltstack之服务管理
  5. mysql-proxy做客户端连接转发【外网访问内网mysql】
  6. zookeeper的python客户端安装
  7. Mysql的学习研究
  8. cocos2d-x 3.0rc1 使用iconv库 解决UTF8乱码问题
  9. Mysql字符串截取函数
  10. python 基础 9.11 更改数据