leetcode刷题记录771-780 python版

前言

继续leetcode刷题生涯
这里记录的都是笔者觉得有点意思的做法
参考了好几位大佬的题解,感谢各位大佬

771. 宝石与石头

class Solution:
    def numJewelsInStones(self, J: str, S: str) -> int:
        if not S or not J: return 0
        import collections
        c = collections.Counter(S)
        res = 0
        for i in J:
            res += c[i]
        return res

773. 滑动谜题

class Solution:
    def slidingPuzzle(self, board: List[List[int]]) -> int:
        import collections
        board = board[0] + board[1]
        visited = {}
        target = [1, 2, 3, 4, 5, 0]
        queue = collections.deque()
        queue.append(board)
        neighbor = {0: [1, 3], 1: [0, 2, 4], 2: [1, 5], 3: [0, 4], 4: [1, 3, 5], 5: [2, 4]}
        step = -1
        while (queue):
            step = step + 1
            length = len(queue)
            while (length > 0):
                length = length - 1
                cur_board = queue.popleft()
                visited[tuple(cur_board)] = True
                if cur_board == target:
                    return step
                zero_index = cur_board.index(0)
                for i in neighbor[zero_index]:
                    cur_board[i], cur_board[zero_index] = cur_board[zero_index], cur_board[i]
                    if not visited.get(tuple(cur_board), None):
                        queue.append(cur_board[:])
                    cur_board[i], cur_board[zero_index] = cur_board[zero_index], cur_board[i]
        return -1

775. 全局倒置与局部倒置

class Solution:
    def isIdealPermutation(self, A: List[int]) -> bool:
        length = len(A)
        index = 0
        while index < length:
            if index != A[index]:
                if index != A[index + 1] or A[index] != index + 1:
                    return False
                else:
                    index += 2
            else:
                index += 1
        return True

777. 在LR字符串中交换相邻字符

class Solution:
    def canTransform(self, start: str, end: str) -> bool:
        n, m = len(start), len(end)
        if n != m: return False
        i, j = 0, 0
        while True:
            while i < n and start[i] == 'X':
                i += 1
            while j < n and end[j] == 'X':
                j += 1
            if i == n or j == n:
                if i == n and j == n:
                    return True
                return False
            if start[i] != end[j]:
                return False
            else:
                if start[i] == 'L' and i < j:
                    return False
                elif start[i] == 'R' and i > j:
                   return False
            i += 1
            j += 1

778. 水位上升的泳池中游泳

class Solution:
    def swimInWater(self, grid: List[List[int]]) -> int:
        '''
        思路:DFS + 二分搜索
        1、dfs输入grid和当前位置(x,y)和水位t,返回是否能到达(n-1,n-1)
        2、找到grid中高度最小值lo和最大值hi
        3、二分查找满足从(0,0)可以dfs到(n-1,n-1)的最低水位
        '''
        def dfs(x, y, t, grid, visited):
            if grid[x][y] > t:
                return False
            if x == y == len(grid)-1:
                return True
            visited[x][y] = True
            ret = False
            for nx, ny in ((x, y-1), (x-1, y), (x+1, y), (x, y+1)):
                if 0 <= nx < len(grid) and 0 <= ny < len(grid) and not visited[nx][ny] and grid[nx][ny] <= t:
                    ret |= dfs(nx, ny, t, grid, visited)
            return ret
        lo = min([min(row) for row in grid])
        hi = max([max(row) for row in grid])
        while lo < hi:
            mi = (lo + hi) // 2
            visited = [[False]*len(grid) for _ in grid]
            reached = dfs(0, 0, mi, grid, visited)
            if reached:
                hi = mi
            else:
                lo = mi + 1
        return lo

779. 第K个语法符号

class Solution:
    def kthGrammar(self, N: int, K: int) -> int:
        if N == 0:
            return 0
        if K % 2:
            return self.kthGrammar(N-1, (K+1) / 2)
        else:
            return abs(self.kthGrammar(N-1, K / 2) - 1)

780. 到达终点

class Solution:
    def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) -> bool:
        if sx == tx and sy == ty: return True
        if sx > tx or sy > ty: return False
        if sx == tx:    # 只能向上走,每步的步长为sx
            return not (ty-sy) % sx
        if sy == ty:    # 只能向右走,每步的步长为sy
            return not (tx-sx) % sy
        return self.reachingPoints(sx, sy, tx-ty, ty) or self.reachingPoints(sx, sy, tx, ty-tx)

你可能感兴趣的:(leetcode,leetcode,python)