力扣hot100——矩阵

73. 矩阵置零

cpp 复制代码
class Solution {
public:
    void setZeroes(vector<vector<int>>& a) {
        int n = a.size(), m = a[0].size();
        vector<int> r(n + 10, 0);
        vector<int> c(m + 10, 0);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (!a[i][j]) {
                    r[i] = 1;
                    c[j] = 1;
                }
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (r[i] || c[j]) a[i][j] = 0;
            }
        }
    }
};

模拟

54. 螺旋矩阵

cpp 复制代码
class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& a) {
        int n = a.size(), m = a[0].size();
        int x = 0, y = 0;
        int sum = m * n;

        int sx = n - 1, sy = m;
        int dx = 1, dy = 1;
        vector<int> ans;
        while (sum) {
            for (int i = 1; i <= sy; i++) {
                ans.push_back(a[x][y]);
                y += dy;
                sum--;
            }
            dy *= -1;
            sy--;
            x += dx;
            y += dy;
            for (int i = 1; i <= sx; i++) {
                ans.push_back(a[x][y]);
                x += dx;
                sum--;
            }
            dx *= -1;
            sx--;
            y += dy;
            x += dx;
        }
        return ans;
    }
};

套路题,模拟

48. 旋转图像

cpp 复制代码
class Solution {
public:
    void rotate(vector<vector<int>>& a) {
        int n = a.size(), m = a[0].size();
        vector<vector<int>> ans(n, vector<int>(m, 0));
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                ans[j][n - i - 1] = a[i][j];
            }
        }
        swap(a, ans);
    }
};

套路题,模拟

240. 搜索二维矩阵 II

cpp 复制代码
class Solution {
public:
    bool searchMatrix(vector<vector<int>>& a, int target) {
        int n = a.size(), m = a[0].size();
        int x = 0, y = m - 1;
        while (x < n && y >= 0) {
            if (a[x][y] == target) return true;
            if (a[x][y] > target) y--;
            else x++;
        }
        return false;
    }
};

Z字形查找

相关推荐
vibecoding日记1 天前
双非如何快速入职字节等大厂大模型?真实案例分析:推理优化和投机解码
算法·求职·大模型工程师
yszaygr21381 天前
Verilog参数化游程编码RLE模块
算法
望易1 天前
刚设计的大模型架构-双域耦合认知框架
算法·架构
复杂网络2 天前
多个 Claude Code 与多个 Codex 协同工作:设计与实现方案
算法
HjhIron2 天前
面试常客:字符串算法从入门到进阶
算法·面试
吴佳浩2 天前
DeepSeek DSpark:Confidence-Scheduled Speculative Decoding 技术解析
人工智能·算法·deepseek
触底反弹2 天前
🧠 搞懂 Token,才算真正入门大模型——从分词原理到 Embedding 语义实战
javascript·人工智能·算法
vivo互联网技术3 天前
ICLR 2026 | 基于后验采样的图像恢复方法LearnIR:人脸去阴影、去雾
人工智能·算法·aigc
浮生望3 天前
JS字符串与回文算法:从包装类到双指针的面试进阶之路
javascript·算法