LeetCode 热题 100 | 矩阵

目录

[1 73. 矩阵置零](#1 73. 矩阵置零)

[2 54. 螺旋矩阵](#2 54. 螺旋矩阵)

[3 48. 旋转图像](#3 48. 旋转图像)

[4 240. 搜索二维矩阵 II](#4 240. 搜索二维矩阵 II)


菜鸟做题第二周,语言是 C++

1 73. 矩阵置零

解题思路:

  1. 遍历矩阵,寻找等于 0 的元素,记录对应的行和列
  2. 将被记录的行的元素全部置 0
  3. 将被记录的列的元素全部置 0
cpp 复制代码
class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        int n = matrix.size(), m = matrix[0].size();
        unordered_set<int> row, col;

        // 寻找0
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (matrix[i][j] == 0) {
                    row.insert(i);
                    col.insert(j);
                }
            }
        }
        
        // 行置0
        for (auto &r:row) {
            for (int j = 0; j < m; ++j) {
                matrix[r][j] = 0;
            }
        }

        // 列置0
        for (auto &c:col) {
            for (int i = 0; i < n; ++i) {
                matrix[i][c] = 0;
            }
        }
    }
};

2 54. 螺旋矩阵

解题思路:

  • 定义 right,down,left,up 来标志四个方向
  • 根据不同的方向设置不同的坐标加减策略
  • 将被遍历过的元素置为 101,用于指示能否继续前进
  • 借助 ans.size() 计数,用于指示是否继续循环

为什么将被遍历过的元素置为 101?

如上图所示,101 是矩阵元素绝对不会取到的数值。

如果题目对取值范围没有限制的话,那可能真的需要定义另一个矩阵来记录遍历情况了。

cpp 复制代码
class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        int right = 1, down = 0, up = 0, left = 0;
        vector<int> ans;
        int n = matrix.size(), m = matrix[0].size();

        int i = 0, j = 0;
        while (ans.size() != n * m) {
            if (right) {
                while (j < m && matrix[i][j] != 101) {
                    ans.push_back(matrix[i][j]);
                    matrix[i][j] = 101;
                    ++j;
                }
                --j;
                ++i;
                right = 0;
                down = 1;
            }

            if (down) {
                while (i < n && matrix[i][j] != 101) {
                    ans.push_back(matrix[i][j]);
                    matrix[i][j] = 101;
                    ++i;
                }
                --i;
                --j;
                down = 0;
                left = 1;
            }

            if (left) {
                while (j >= 0 && matrix[i][j] != 101) {
                    ans.push_back(matrix[i][j]);
                    matrix[i][j] = 101;
                    --j;
                }
                ++j;
                --i;
                left = 0;
                up = 1;
            }

            if (up) {
                while (i >= 0 && matrix[i][j] != 101) {
                    ans.push_back(matrix[i][j]);
                    matrix[i][j] = 101;
                    --i;
                }
                ++i;
                ++j;
                up = 0;
                right = 1;
            }
        }
        return ans;
    }
};

3 48. 旋转图像

报一丝,还是用了新的矩阵,以后想想其他办法。。。

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

4 240. 搜索二维矩阵 II

笨办法,但意外的是没超时

cpp 复制代码
class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {

        int n = matrix.size(), m = matrix[0].size();

        int i = 0, j = 0;
        while (i < n && j < m && matrix[i][j] <= target) {
            if (matrix[i][j] == target) return true;
            ++i;
            ++j;
        }

        // 针对n<m且找到头的情况
        if (i == n) {
            --i;
            while (j < m && matrix[i][j] <= target) {
                if (matrix[i][j] == target) return true;
                ++j;
            }
            if (j == m) return false;

            for (int y = j; y < m; ++y) {
                for (int x = i; x >= 0; --x) {
                    if (matrix[x][y] == target) return true;
                }
            }
        }

        // 针对n>m的情况且找到头的情况
        if (j == m) {
            --j;
            while (i < n && matrix[i][j] <= target) {
                if (matrix[i][j] == target) return true;
                ++i;
            }
            if (i == n) return false;

            for (int x = i; x < n; ++x) {
                for (int y = j; y >= 0; --y) {
                    if (matrix[x][y] == target) return true;
                }
            }
        }

        for (int x = i; x < n; ++x) {
            for (int y = j; y >= 0; --y) {
                if (matrix[x][y] == target) return true;
            }
        }

        for (int y = j; y < m; ++y) {
            for (int x = i; x >= 0; --x) {
                if (matrix[x][y] == target) return true;
            }
        }
        
        return false;
    }
};
相关推荐
YuCaiH25 分钟前
数组理论基础
笔记·leetcode·c·数组
2301_807611491 小时前
77. 组合
c++·算法·leetcode·深度优先·回溯
SsummerC2 小时前
【leetcode100】零钱兑换Ⅱ
数据结构·python·算法·leetcode·动态规划
好易学·数据结构3 小时前
可视化图解算法:二叉树的最大深度(高度)
数据结构·算法·二叉树·最大高度·最大深度·二叉树高度·二叉树深度
程序员-King.3 小时前
day47—双指针-平方数之和(LeetCode-633)
算法·leetcode
阳洞洞3 小时前
leetcode 1035. Uncrossed Lines
算法·leetcode·动态规划·子序列问题
小鹿鹿啊4 小时前
C语言编程--15.四数之和
c语言·数据结构·算法
rigidwill6664 小时前
LeetCode hot 100—最长有效括号
数据结构·c++·算法·leetcode·职场和发展
wuqingshun3141595 小时前
蓝桥杯17. 机器人塔
c++·算法·职场和发展·蓝桥杯·深度优先