LeetCode | 算法:矩阵置0、删除重复邮箱

73. Set Matrix Zeroes

Given a n x m matrix, if an element is 0, set its entire row and column to 0. Do it in-place.
Example :
Input:
[ [0,1,2,0],
[3,4,5,2],
[1,3,1,5] ]
Output:
[ [0,0,0,0],
[0,4,5,0],
[0,3,1,0] ]
笔记:
(1)分别用两个集合记录下含0的行号和列号,然后再修改矩阵。
(2)时间复杂度O(nm),空间复杂度O(n+m)。

class Solution:
    def setZeroes(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: void Do not return anything, modify matrix in-place instead.
        """
        # 先找到含0的行列
        zero_row = set()
        zero_col = set()
        
        n = len(matrix)     #行数
        m = len(matrix[0])  #列数
        
        for row in range(n):
            for col in range(m):
                if matrix[row][col] == 0:
                    zero_row.add(row)
                    zero_col.add(col)
        
        #好啦,现在我们来把含0的行列置为0
        for row in range(n):
            if row in zero_row:
                matrix[row] = [0] * m
            for col in zero_col:
                matrix[row][col] = 0

196. Delete Duplicate Emails【这道是SQL,惊不惊喜意不意外!】

Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id.

6778119-9aa76eb8e7e81367.png
举个栗子.png

笔记:
select distinct Email没用的,select只是查询,这里要改表,要用delete.

# MySQL
# solution 1(faster)
DELETE p1 FROM Person p1,Person p2
WHERE p1.Email = p2.Email AND p1.Id > p2.Id;

# solution 2(slower but easier to understand)
DELETE FROM Person 
WHERE Id NOT IN 
     (SELECT MIN([p.Id]) FROM (SELECT * FROM Person) p
     Group by p.Email);

猜你喜欢

转载自blog.csdn.net/weixin_34067980/article/details/87093996