Algorithm: Array(1)

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;
            }
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值