0
点赞
收藏
分享

微信扫一扫

DAY21-T1219&T408-2022-02-05-非自己作答

精进的医生 2022-02-05 阅读 33
class Solution:
    def getMaximumGold(self, grid: List[List[int]]) -> int:
        m, n = len(grid), len(grid[0])
        ans = 0

        def dfs(x: int, y: int, gold: int) -> None:
            gold += grid[x][y]
            nonlocal ans
            ans = max(ans, gold)

            rec = grid[x][y]
            grid[x][y] = 0

            for nx, ny in ((x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)):
                if 0 <= nx < m and 0 <= ny < n and grid[nx][ny] > 0:
                    dfs(nx, ny, gold)

            grid[x][y] = rec

        for i in range(m):
            for j in range(n):
                if grid[i][j] != 0:
                    dfs(i, j, 0)

        return ans

# 作者:LeetCode-Solution
# 链接:https://leetcode-cn.com/problems/path-with-maximum-gold/solution/huang-jin-kuang-gong-by-leetcode-solutio-f9gg/
class Solution:
    def validWordAbbreviation(self, word: str, abbr: str) -> bool:
        abbr_equal_len = 0
        num = 0
        for c in abbr:
            if c.isdigit() != 0:    #是个数字
                if num == 0 and c == '0':   #不能02 002
                    return False
                num = num * 10 + int(c)
            else:               #是个字母
                abbr_equal_len += num   #先把前面的数字搞定
                num  = 0
                abbr_equal_len += 1     #再算上当前的这个字母
                if abbr_equal_len > len(word) or c != word[abbr_equal_len - 1]:
                    return False
        abbr_equal_len += num       #abbr最后是个数字的情况

        return abbr_equal_len == len(word)

# 作者:Hanxin_Hanxin
# 链接:https://leetcode-cn.com/problems/valid-word-abbreviation/solution/c-python3-yi-suo-xie-abbrwei-dui-xiang-j-5leb/
举报

相关推荐

0 条评论