leetcode 刷题 130 131

举报
毛利 发表于 2021/07/15 06:45:24 2021/07/15
【摘要】 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

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。