每日算法刷题 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

相关推荐
张欣-男21 分钟前
3分钟理解线性代数
线性代数·算法
白狐_79825 分钟前
408 数据结构|外部排序优化:怎么减少时间开销
数据结构·算法
小白说大模型25 分钟前
Hermes 全配置指南:从裸版到 AI Agent 天花板
大数据·人工智能·学习·算法·机器学习·数据挖掘
a1879272183136 分钟前
【算法】动态规划入门:从打家劫舍到状态设计的三堂课
算法·leetcode·动态规划·dp·回溯·暴力
淡海水10 小时前
07-04-并发-ConcurrentBag-T-工作窃取WorkStealing算法
开发语言·算法·c#·bag·concurrent·workstealing
leobertlan11 小时前
好玩系列:训练一个神经网络模型指导小孩玩游戏2-大局观教练
算法
Tisfy11 小时前
LeetCode 3876.构造奇偶一致的数组 II:三种情况分类讨论(其实还是脑筋急转弯)
算法·leetcode·题解·脑筋急转弯
乐迪信息12 小时前
智慧港口船舶AI算法实现在线状态监测
大数据·人工智能·深度学习·算法·计算机视觉
木井巳14 小时前
【BFS/DFS 解决 FloodFill 算法】太平洋大西洋水流问题
java·算法·leetcode·深度优先·广度优先·宽度优先·推荐算法
心抵鹊14 小时前
归并排序之翻转对(hard)
数据结构·算法