数据规模->时间复杂度
<=10^4 😮(n^2)
<=10^7:o(nlogn)
<=10^8:o(n)
10^8<=:o(logn),o(1)
内容
lc 401 :二进制手表
https://leetcode.cn/problems/binary-watch/
提示:
0 <= turnedOn <= 10
class Solution:
def readBinaryWatch(self, turnedOn: int) -> List[str]:
nums1 = [8, 4, 2, 1]
nums2 = [32, 16, 8, 4, 2, 1]
#
res=[]
for i in range(turnedOn+1): #例如0:15
hours,minutes=self.findcombs(nums1,i),self.findcombs(nums2,turnedOn-i)
for hour in hours:
if hour>11:continue
for minute in minutes:
if minute>59:continue
minute_str='0' + str(minute) if minute<10 else str(minute)
res.append(str(hour)+':'+minute_str)
return res
def findcombs(self,nums,cnt):
combs=[]
self.dfs(nums,cnt,0,0,combs)
return combs
def dfs(self,nums,cnt,sum,startindex,combs):
if cnt==0:
combs.append(sum)
return
for i in range(startindex,len(nums)):
self.dfs(nums,cnt-1,sum+nums[i],i+1,combs) #’组合层‘
lc 131【剑指 086】 :分割回文串
https://leetcode.cn/problems/palindrome-partitioning/
提示:
1 <= s.length <= 16
s 仅由小写英文字母组成
class Solution:
def partition(self, s: str) -> List[List[str]]:
path=[]
self.res=[]
self.dfs(s,0,path)
return self.res
def dfs(self,s,startindex,path):
if startindex==len(s):
self.res.append(path[:])
return
for i in range(startindex,len(s)):
if not self.is_partition(s,startindex,i):continue
#
path.append(s[startindex:i+1])
self.dfs(s,i+1,path)
path.pop()
def is_partition(self,strs,left,right):
while left<right:
if strs[left]!=strs[right]:
return False
left,right=left+1,right-1
return True
lc 79 【剑指 12】【top100】: 单词搜索
https://leetcode.cn/problems/word-search/
提示:
m == board.length
n = board[i].length
1 <= m, n <= 6
1 <= word.length <= 15
board 和 word 仅由大小写英文字母组成
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
dirs=[[-1,0],[1,0],[0,-1],[0,1]]
rows,cols=len(board),len(board[0])
visited=[[False]*cols for _ in range(rows)]
#
def dfs(row,col,index):
if board[row][col] != word[index]:return False
elif index==len(word)-1:return True
visited[row][col]=True
for d in dirs:
nextrow=row+d[0]
nextcol=col+d[1]
if 0<=nextrow<rows and 0<=nextcol<cols and not visited[nextrow][nextcol]:
if dfs(nextrow,nextcol,index+1):return True
##回溯
visited[row][col]=False #key:运行到这,说明dfs(nextrow,nextcol,index+1)返回False,要撤回,回溯
#
for row in range(rows):
for col in range(cols):
if board[row][col]==word[0]:#遍历起始点
if dfs(row,col,0):
return True
return False
lc 301【top100】:删除无效的括号
https://leetcode.cn/problems/remove-invalid-parentheses/
提示:
1 <= s.length <= 25
s 由小写英文字母以及括号 ‘(’ 和 ‘)’ 组成
s 中至多含 20 个括号
class Solution:
def removeInvalidParentheses(self, s: str) -> List[str]:
queue=deque()
visited=set()
res=[]
#
queue.append(s)
visited.add(s)
level_find=False #如当前层存在有效字符,break->保证删除最小数量
while queue:
#处理当前层
for i in range(len(queue)):
curr_strs=queue.popleft()
if self.is_valid(curr_strs):
res.append(curr_strs)
level_find=True
continue
#下一层准备(删-j位)
for j in range(len(curr_strs)):
if curr_strs[j] != '(' and curr_strs[j] != ')':continue #key:存在字母
left_strs=curr_strs[0:j]
right_strs=curr_strs[j+1:len(curr_strs)] if j != len(curr_strs)-1 else ''
next_strs=left_strs+right_strs
if next_strs not in visited:
visited.add(next_strs)
queue.append(next_strs)
#key
if level_find:break
return res
def is_valid(self,strs):
cnt=0
for c in strs:
if c=="(":cnt+=1
elif c==')':cnt-=1
#
if cnt<0:return False
return cnt==0