每日算法刷题 Day3 5.11:leetcode数组2道题,用时1h(有点慢)

5.LC 零矩阵(中等)

面试题 01.08. 零矩阵 - 力扣(LeetCode)

思想:

法一 :

利用两个集合分别储存要清0的行和列索引

另外两种原地优化空间的做法暂时不是目前刷题目标,故不考虑

代码

c++:

复制代码
class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        set<int> hang;
        set<int> lie;
        int n = matrix.size(), m = matrix[0].size();
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (matrix[i][j] == 0) {
                    hang.insert(i);
                    lie.insert(j);
                }
            }
        }
        for (const auto& x : hang) {
            for (int j = 0; j < m; ++j) {
                matrix[x][j] = 0;
            }
        }
        for (const auto& x : lie) {
            for (int i = 0; i < n; ++i) {
                matrix[i][x] = 0;
            }
        }
    }
};

python:

复制代码
class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        hang = set()
        lie = set()
        n, m = len(matrix), len(matrix[0])
        for i in range(n):
            for j in range(m):
                if matrix[i][j] == 0:
                    hang.add(i)
                    lie.add(j)
        for x in hang:
            for j in range(m):
                matrix[x][j] = 0
        for x in lie:
            for i in range(n):
                matrix[i][x] = 0
相似题

73. 矩阵置零 - 力扣(LeetCode)

6.LC 对角线遍历(中等,学习)

498. 对角线遍历 - 力扣(LeetCode)

思想:

1.n行m列矩阵,因为是按照对角线遍历,但是分析可得有两种对角线遍历方式,从左下到右上和从右上到左下,是由第i条对角线的奇偶决定的,所以按照对角线遍历, i ∈ [ 0 , n + m − 1 ) i \in [0,n+m-1) i∈[0,n+m−1)

2.对于偶数对角线,是从左下到右上的遍历顺序,但又会出现遍历初始位置不同的情况,故再分类

  • i<n,说明在左边界,初始位置(i,0)
  • i>=n,说明在下边界,初始位置(n-1,i-(n-1))即(n-1,i-n+1)
    3.奇数对角线,是从右上到左下的遍历顺序,对偶式即可
  • i<m,说明在上边界,初始位置(0,i)
  • i>=m,说明在右边界,初始位置(i-m+1,m-1)
代码

c++:

复制代码
class Solution {
public:
    vector<int> findDiagonalOrder(vector<vector<int>>& mat) {
        vector<int> res;
        int n = mat.size(), m = mat[0].size();
        for (int i = 0; i < n + m - 1; ++i) {
            // 右上到左下
            if (i % 2) {
                int x = i < m ? 0 : i - m + 1;
                int y = i < m ? i : m - 1;
                while (x < n && y >= 0) {
                    res.emplace_back(mat[x][y]);
                    x++;
                    y--;
                }
            }
            // 左下到右上
            else {
                int x = i < n ? i : n - 1;
                int y = i < n ? 0 : i - n + 1;
                while (y < m && x >= 0) {
                    res.emplace_back(mat[x][y]);
                    x--;
                    y++;
                }
            }
        }
        return res;
    }
};

python:

复制代码
class Solution:
    def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
        n, m = len(mat), len(mat[0])
        res = []
        for i in range(n + m - 1):
            if i % 2 == 1:
                x = 0 if i < m else i - m + 1
                y = i if i < m else m - 1
                while x < n and y >= 0:
                    res.append(mat[x][y])
                    x += 1
                    y -= 1
            else:
                y = 0 if i < n else i - n + 1
                x = i if i < n else n - 1
                while y < m and x >= 0:
                    res.append(mat[x][y])
                    y += 1
                    x -= 1
        return res
        
class Solution:
    def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
        n,m=len(mat),len(mat[0])
        res=[]
        for i in range(n+m-1):
            if i%2==1:
                x=0 if i<m else i-m+1
                y=i if i<m else m-1
                while x<n and y>=0:
                    res.append(mat[x][y])
                    x+=1
                    y-=1
            else:
                y=0 if i<n else i-n+1
                x=i if i<n else n-1
                while y<m and x>=0:
                    res.append(mat[x][y])
                    y+=1
                    x-=1
        return res

1.没有三目运算符,要写成x=0 if i<m else i-m+1

2.没有++运算符,要写成x+=1

相关推荐
noipp4 分钟前
推荐题目:洛谷 P6231 [JSOI2013] 公交系统
c语言·数据结构·c++·算法·游戏·洛谷·luogu
c2385618 分钟前
C/C++每日一练6
c语言·c++·算法
AI小码1 小时前
把动作「画」给视频世界模型,跨本体双向推演,李飞飞参与
大数据·人工智能·算法·ai·大模型·音视频·编程
KaMeidebaby2 小时前
卡梅德生物技术快报|bli亲和力检测gst:告别批量跑胶:BLI实时酶切监测技术加速GST融合蛋白下游流程优化
前端·网络·数据库·人工智能·算法
alphaTao2 小时前
LeetCode 每日一题 2026/7/20-2026/7/26
算法·leetcode
圣保罗的大教堂3 小时前
leetcode 3514. 不同 XOR 三元组的数目 II 中等
leetcode
青山木6 小时前
Hot 100 ---腐烂的橘子
java·数据结构·后端·算法·leetcode·广度优先
未来之窗软件服务6 小时前
计算机考试-快速排序—东方仙盟
数据结构·算法·排序算法·仙盟创梦ide·东方仙盟
小龙报6 小时前
【优选算法】1. 水果成蓝 2.找到字符串中所有字母的异位词
java·c语言·数据结构·数据库·c++·redis·算法
txzrxz7 小时前
数论:排列数、组合数、费马小定理、逆元、同余定理
c++·算法·数论·组合数·费马小定理·逆元·排列数