leetcode 刷题 130 131
【摘要】
class Solution: def solve(self, board: List[List[str]]) -> None: """ Do not return anything, modify board in-place instead. """ if not any(board): return m, n = len(board), len(boa...


class Solution: def solve(self, board: List[List[str]]) -> None: """ Do not return anything, modify board in-place instead. """ if not any(board): return m, n = len(board), len(board[0]) save = [ij for k in range(m+n) for ij in ((0, k), (m-1, k), (k, 0), (k, n-1))] while save: i, j = save.pop() if 0 <= i < m and 0 <= j < n and board[i][j] == 'O': board[i][j] = 'S' save += (i, j-1), (i, j+1), (i-1, j), (i+1, j) board[:] = [['XO'[c == 'S'] for c in row] for row in board]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
class Solution: def solve(self, board: List[List[str]]) -> None: """ Do not return anything, modify board in-place instead. """ if not board: return lenth, height, stack=len(board[0]), len(board), [] q=[[(0, height-1), [i for i in range(lenth)]],[[j for j in range(height)],(0, lenth-1)]] while q: ii, jj=q.pop() for i in ii: for j in jj: if board[i][j]=='O': stack+=(i,j), while stack: i,j=stack.pop() if 0<=i<height and 0<=j<lenth and board[i][j]=='O': board[i][j]='#' stack+=(i+1,j),(i,j+1),(i-1,j),(i,j-1), for i in range(height): for j in range(lenth): if board[i][j]=='O': board[i][j]='X' if board[i][j]=='#': board[i][j]='O
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23


class Solution: def partition(self, s: str) -> List[List[str]]: # if not s: return [[]] # res = [] # for i in range(len(s)): # temp = s[:i+1] # if temp == temp[::-1]: # for j in self.partition(s[i+1:]): # res.append([temp]+j) # return res res = [] self.dfs(s, [], res) return res def dfs(self, s, path, res): if not s: res.append(path) return for i in range(1, len(s)+1): if self.isPal(s[:i]): self.dfs(s[i:], path+[s[:i]], res) def isPal(self, s): return s == s[::-1]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
class Solution: def partition(self, s: str) -> List[List[str]]: return [[s[:i]] + rest for i in range(1, len(s)+1) if s[:i] == s[i-1::-1] for rest in self.partition(s[i:])] or [[]]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
文章来源: maoli.blog.csdn.net,作者:刘润森!,版权归原作者所有,如需转载,请联系作者。
原文链接:maoli.blog.csdn.net/article/details/90743050
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)