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;
    }
}
相关推荐
蒟蒻小袁13 分钟前
力扣面试150题--被围绕的区域
leetcode·面试·深度优先
Q81375746037 分钟前
中阳视角下的资产配置趋势分析与算法支持
算法
yvestine44 分钟前
自然语言处理——文本表示
人工智能·python·算法·自然语言处理·文本表示
GalaxyPokemon1 小时前
LeetCode - 148. 排序链表
linux·算法·leetcode
iceslime2 小时前
旅行商问题(TSP)的 C++ 动态规划解法教学攻略
数据结构·c++·算法·算法设计与分析
aichitang20242 小时前
矩阵详解:从基础概念到实际应用
线性代数·算法·矩阵
OpenCSG3 小时前
电子行业AI赋能软件开发经典案例——某金融软件公司
人工智能·算法·金融·开源
chao_7894 小时前
链表题解——环形链表 II【LeetCode】
数据结构·leetcode·链表
dfsj660114 小时前
LLMs 系列科普文(14)
人工智能·深度学习·算法
薛定谔的算法4 小时前
《盗梦空间》与JavaScript中的递归
算法