Algorithm: Array(1)
来源:互联网 发布:网络教育发展趋势 编辑:程序博客网 时间:2024/06/11 03:18
54. Spiral Matrix
59. Spiral Matrix II
73. Set Matrix Zeroes
54. 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].
思路:这道题的pattern是利用int row; int col; 接着每次就用 for (int i = 0; i < row; i++)去填充某一个行、某一列之后,row--(刚刚填充了一行)或者col--(刚刚填充了一列)。接着判断停止的标准是row == 0或者 col == 0。注意是填充的变量,因为是优先填充第一行,所以col填充变量要从-1开始。如果优先填充一列,row填充变量从0开始。
public class Solution { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> ans = new ArrayList<>(); if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) { return ans; } int row = matrix.length; int col = matrix[0].length; int rowPos = 0; int colPos = -1; while (true) { for (int t = 0; t < col; t++) { ans.add(matrix[rowPos][++colPos]); } if (--row == 0) { break; } // 因为上面已经自减了,所以是row = row - 1了。 for (int t = 0; t < row; t++) { ans.add(matrix[++rowPos][colPos]); } if (--col == 0) { break; } for (int t = 0; t < col; t++) { ans.add(matrix[rowPos][--colPos]); } if (--row == 0) { break; } for (int t = 0; t < row; t++) { ans.add(matrix[--rowPos][colPos]); } if (--col == 0) { break; } } return ans; }}
59. Spiral Matrix II
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 class Solution { public int[][] generateMatrix(int n) { if (n <= 0) { return new int[0][0]; } int[][] res = new int[n][n]; int row = 0; int col = -1; int colNum = n; int rowNum = n; int num = 0; while (true) { for (int t = 0; t < colNum; t++) { res[row][++col] = ++num; } if (--rowNum == 0) { break; } for (int t = 0; t < rowNum; t++) { res[++row][col] = ++num; } if (--colNum == 0) { break; } for (int t = 0; t < colNum; t++) { res[row][--col] = ++num; } if (--rowNum == 0) { break; } for (int t = 0; t < rowNum; t++) { res[--row][col] = ++num; } if (--colNum == 0) { break; } } return res; }}
73. Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
click to show follow up.
Follow up:
Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?
思路:如代码comment. 同时,需要考虑的是,在最后动到第0行或者第0列之前,任何操作都不能动到第0行或者第0列。
public class Solution { // 题目的最简单方法如下: // 利用第0行和第0列作为某行某列是否应该设为0的依据。因为,在同一行当中,任何列为0,正行为0。所以,第0列或者第n列为0的效果 // 是一样的。所以,利用这个原理,第0行和第0列单独处理记录。接着扫描剩余的所有格。一旦一个格子为0,就把对应的0行、0列的元 // 素置为零。 // 然后根据扫描后的零行零列记录,置对应的列和行为0后。根据原来0行、0列的记录,给对应的0行、0列置零。 public void setZeroes(int[][] matrix) { if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) { return; } boolean rowFlag = false; boolean colFlag = false; int colNum = matrix[0].length; int rowNum = matrix.length; for (int i = 0; i < colNum; i++) { if (matrix[0][i] == 0) { rowFlag = true; break; } } for (int j = 0; j < rowNum; j++) { if (matrix[j][0] == 0) { colFlag = true; break; } } for (int i = 1; i < rowNum; i++) { for (int j = 1; j < colNum; j++) { if (matrix[i][j] == 0) { matrix[0][j] = 0; matrix[i][0] = 0; } } } for (int i = 1; i < colNum; i++) {//注意是从1开始,不能碰第0列 if (matrix[0][i] == 0) { for (int k = 1; k < rowNum; k++) { matrix[k][i] = 0; } } } for (int j = 1; j < rowNum; j++) {//注意是从1开始,不能碰第0行 if (matrix[j][0] == 0) { for (int k = 1; k < colNum; k++) { matrix[j][k] = 0; } } } //返回处理第0行,第0列 if (rowFlag) { for (int k = 0; k < colNum; k++) { matrix[0][k] = 0; } } if (colFlag) { for (int k = 0; k < rowNum; k++) { matrix[k][0] = 0; } } }}
- Algorithm: Array(1)
- [Algorithm]九章六之二: Array
- Algorithm - Binary Search on 2D Array
- 【Leetcode Algorithm】Remove Duplicates from Sorted Array
- Algorithm(1)
- 算法库algorithm-1-algorithm
- Algorithm: Reverse a Integer to an array with recursion
- Shuffle an Array or a List - Algorithm in Java
- Algorithm: efficient way to remove duplicate integers from an array
- Leetcode Algorithm 215. Kth Largest Element in an Array
- Algorithm-Arrays-5 Repeat and Missing Number Array
- 【论文 Frost1972】An Algorithm for Linearly Constrained Adaptive Array Processing
- Algorithm之路二十六:Remove Duplicates from Sorted Array
- string algorithm 1
- Algorithm in Python(1)
- algorithm—for_each() (1)
- PageRank Algorithm (1)
- 《Algorithm》读书笔记-1
- Java IO示例代码
- tensorflow 练习
- 为什么电脑WiFi图标会有一把红色的叉叉在上面?
- java-命令
- 与计算机的第一次对话“Hello world”
- Algorithm: Array(1)
- 转行穷三年?未必!
- bzoj 1656 bfs+射线法
- 友盟QQ第三方授权登陆(精简)
- lua变动参数累加_简单
- 漫谈Android移动应用软件测试
- 【linux】linux中各文件夹的作用
- 数据库三大范式
- MFC Windows程序设计学习笔记--滚动条