leetcode top100矩阵题73.54.48.240

73. 矩阵置零
复制代码
class Solution {
    public void setZeroes(int[][] matrix) {
        int line = matrix.length;
        int col = matrix[0].length;
        List<Integer> changeLines = new ArrayList<Integer>();
        List<Integer> changeCols = new ArrayList<Integer>();
        for(int i = 0; i < line; i++) {
            for(int j = 0; j < col; j++) {
                if (matrix[i][j] == 0) {
                    changeLines.add(i);
                    changeCols.add(j);
                }
            }
        }
​
        for(int i = 0; i < line; i++) {
            if (changeLines.contains(i)) {
                Arrays.fill(matrix[i], 0);
            }
            for(int j = 0; j < changeCols.size(); j++) {
                matrix[i][changeCols.get(j)] = 0;
            }
            
        }
        return;
    }
}
54. 螺旋矩阵
复制代码
class Solution {
    private static final int[][] DIRS = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
    public List<Integer> spiralOrder(int[][] matrix) {
        int line = matrix.length;
        int col = matrix[0].length;
        int size = line * col;
        List<Integer> ans = new ArrayList<>(line * col);
        int i = 0; 
        int j = -1;
        for(int di = 0; ans.size() < size; di = (di + 1) % 4) {
            for(int k = 0; k < col; k++) {
                i += DIRS[di][0];
                j += DIRS[di][1];
                ans.add(matrix[i][j]);
            }
            int tmp = col;
            col = line - 1;
            line = tmp;
        }
        return ans;
    }
}
48. 旋转图像
复制代码
class Solution {
    public void rotate(int[][] matrix) {
        int lines = matrix.length;
        int cols = matrix[0].length;
        int[][] copy = new int[lines][cols];
        for(int i = 0; i < lines; i++) {
            for(int j = 0; j < cols; j++) {
                copy[i][j] = matrix[i][j];
            }
        }
        for(int i = 0; i < lines; i++) {
            for(int j = 0; j < cols; j++) {
                matrix[j][cols - i - 1] = copy[i][j];
            }
        }
        return;
    }
}
240. 搜索二维矩阵Ⅱ
复制代码
class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int m = matrix.length;
        int n = matrix[0].length;
        for(int i = 0; i < m; i++) {
            if (matrix[i][n - 1] < target) {
                continue;
            } else if (matrix[i][n - 1] == target) {
                return true;
            }
            for(int j = n - 1; j >= 0; j--) {
                if (matrix[i][j] > target) {
                    continue;
                } else if (matrix[i][j] == target) {
                    return true;
                } else {
                    break;
                }
            }
        }
        return false;
    }
}
相关推荐
MM_MS2 小时前
Halcon变量控制类型、数据类型转换、字符串格式化、元组操作
开发语言·人工智能·深度学习·算法·目标检测·计算机视觉·视觉检测
独自破碎E2 小时前
【二分法】寻找峰值
算法
mit6.8243 小时前
位运算|拆分贪心
算法
ghie90903 小时前
基于MATLAB的TLBO算法优化实现与改进
开发语言·算法·matlab
恋爱绝缘体13 小时前
2020重学C++重构你的C++知识体系
java·开发语言·c++·算法·junit
wuk9983 小时前
VSC优化算法MATLAB实现
开发语言·算法·matlab
Z1Jxxx4 小时前
加密算法加密算法
开发语言·c++·算法
乌萨奇也要立志学C++4 小时前
【洛谷】递归初阶 三道经典递归算法题(汉诺塔 / 占卜 DIY/FBI 树)详解
数据结构·c++·算法
vyuvyucd4 小时前
C++引用:高效编程的别名利器
算法
鱼跃鹰飞5 小时前
Leetcode1891:割绳子
数据结构·算法