leetcode
来源:互联网 发布:三国志11画质优化补丁 编辑:程序博客网 时间:2024/06/01 07:51
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]
.
Solution:
public List<Integer> spiralOrder(int[][] matrix) { List<Integer> result = new ArrayList<Integer>(); if (matrix.length == 0) { return result; } int rowBegin = 0; int rowEnd = matrix.length - 1; int colBegin = 0; int colEnd = matrix[0].length - 1; while (rowBegin <= rowEnd && colBegin <= colEnd) { for (int j = colBegin; j <= colEnd; j++) { result.add(matrix[rowBegin][j]); } rowBegin++; for (int j = rowBegin; j <= rowEnd; j++) { result.add(matrix[j][colEnd]); } colEnd--; if (rowBegin <= rowEnd) { for (int j = colEnd; j >= colBegin; j--) { result.add(matrix[rowEnd][j]); } } rowEnd--; if (colBegin <= colEnd) { for (int j = rowEnd; j >= rowBegin; j--) { result.add(matrix[j][colBegin]); } } colBegin++; } return result; }
0 0
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- leetcode
- [乱搞 思路题] BZOJ 2722 [Violet 5]爱的花环
- 关于输入框的值
- Caffe学习(4)——caffe中的激活层及对应参数
- 【图像处理】JPEG Tags
- RabbitMQ使用场景
- leetcode
- PAT 1126
- Mybatis类型转换TypeHandler介绍
- 程序调用ShellExecuteEx打开其他程序(兼容UAC获取管理员权限)
- 近20个绚丽实用的jQuery/CSS3侧边栏菜单
- leetcode
- 平凡
- 机器人操作系统ROS Indigo 入门学习(6)——理解ROS Nodes
- 从零开始部署小型企业级虚拟桌面 -- Vmware Horizon View 6 For Linux VDI -- 概念简介