https://leetcode-cn.com/conte...

第一题 5344. 有多少小于当前数字的数字

https://leetcode-cn.com/conte...

我的做法是先排序,然后用 dict 统计每个数字前面的数字个数。然后遍历 nums 生成答案。

class Solution:
    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
        slist = nums[::]
        slist.sort()
        d = {}
        i = 0
        cur = slist[0]
        d[cur] = 0
        while i < len(slist):
            if slist[i] == cur:
                pass
            else:
                cur = slist[i]
                d[slist[i]] = i
            i += 1
        r = []
        for n in nums:
            r.append(d[n])
        return r

第二题 5345. 通过投票对团队排名

https://leetcode-cn.com/conte...

rank 是一个 list,对每一个队伍的第一位到第 n 位排名做了统计,直接用 sort 方法排序即可。

class Solution:
    def rankTeams(self, votes: List[str]) -> str:
        l = len(votes[0])
        rank = []
        d = {}
        for t in votes[0]:
            d[t] = [0] * l + [-ord(t)]
        for v in votes:
            for i, t in enumerate(v):
                d[t][i] += 1
        for k in d:
            rank.append(d[k])
        # print(rank)
        rank.sort(reverse=True)
        r = []
        for x in rank:
            r.append(chr(-x[-1]))
        return ''.join(r)

第三题 5346. 二叉树中的列表

https://leetcode-cn.com/conte...

dfs 函数用于遍历树,match 函数用于在树的某个节点与链表头节点的值相等时比较树和链表的值,如果匹配成功了,则 falg 设为 True,之后就不再遍历和比较了。

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    
    def dfs(self, h, r):
        if self.flag:
            return
        if h.val == r.val:
            self.match(h, r)
            if self.flag:
                return
        if r.left:
            self.dfs(h, r.left)
        if not self.flag:
            if r.right:
                self.dfs(h, r.right)
        
    def match(self, h, r):
        if r and h.val == r.val:
            # print('mathch', h.val)
            if h.next == None:
                self.flag = True
                return
            self.match(h.next, r.left)
            if not self.flag:
                self.match(h.next, r.right)
        else:
            return
    
    def isSubPath(self, head: ListNode, root: TreeNode) -> bool:
        self.flag = False
        self.dfs(head, root)
        return self.flag

第四题 5347. 使网格图至少有一条有效路径的最小代价

https://leetcode-cn.com/conte...

广度优先搜索,每一步都按照原来格子的方向走完所有的地方。

下一步从上一步的边缘,访问任何相邻但是没有访问过的格子

class Solution:
    def minCost(self, grid: List[List[int]]) -> int:
        # 1 ,下一步往右走,也就是你会从 grid[i][j] 走到 grid[i][j + 1]
        # 2 ,下一步往左走,也就是你会从 grid[i][j] 走到 grid[i][j - 1]
        # 3 ,下一步往下走,也就是你会从 grid[i][j] 走到 grid[i + 1][j]
        # 4 ,下一步往上走,也就是你会从 grid[i][j] 走到 grid[i - 1][j]
        n = len(grid)
        m = len(grid[0])
        mp = [[False] *m for _ in range(n)]
        def step(x, y):
            l = set()
            while 0 <= x < n and 0 <= y < m:
                if (x, y) in l or mp[x][y]:
                    break
                mp[x][y] = True
                l.add((x, y))
                if (x, y) == (n-1, m-1): return l
                if grid[x][y] == 1:
                    y += 1
                elif grid[x][y] == 2:
                    y -= 1
                elif grid[x][y] == 3:
                    x += 1
                elif grid[x][y] == 4:
                    x -= 1
            return l
        s1 = step(0, 0)
        if (n-1, m-1) in s1:
            return 0
        d = 0
        while True:
            ss = set()
            for x, y in s1:
                dr = [[0, 1], [1,0], [-1, 0], [0, -1]]
                for xd, yd in dr:
                    xx = x + xd
                    yy = y + yd
                    if 0<= xx < n and 0 <= yy < m and not mp[xx][yy]:
                        s2 = step(xx, yy)
                        if (n-1, m-1) in s2:
                            return d + 1
                        ss = ss.union(s2)
            s1 = ss
            d += 1
        return 

欢迎来我的博客: https://codeplot.top/
我的博客刷题分类:https://codeplot.top/categories/%E5%88%B7%E9%A2%98/


sxwxs
292 声望20 粉丝

计算机专业学生