新学的点
递归时,直接在函数里变化 回溯的变化可以不用写 如下边代码#位置
class Solution:
def letterCasePermutation(self, s: str) -> List[str]:
def bt(s,temp,index):
if len(s) == index:
res.append(temp)
return
if s[index].isdigit():
temp+=s[index]
bt(s,temp,index+1)
else:
if s[index].islower():
bt(s,temp+s[index].upper(),index+1)
else:
bt(s,temp+s[index].lower(),index+1)
bt(s,temp+s[index],index+1)#回溯时,并没有写回溯 而是直接,temp+s[index],因为在实际运行中temp这个变量并没有在该层进行变换
res = []
bt(s,'',0)
return res
写了回溯条件的代码
#号位置回溯
class Solution:
def letterCasePermutation(self, s: str) -> List[str]:
def bt(s,temp,index):
if len(s) == index:
res.append(temp)
return
if s[index].isdigit():
temp+=s[index]
bt(s,temp,index+1)
else:
if s[index].islower():
temp+=s[index].upper()
bt(s,temp,index+1)
else:
temp+=s[index].lower()
bt(s,temp,index+1)
temp = temp[:-1] #删除添加进来的倒数元素
temp+=s[index] #添加没有变化的大小写的s里的元素
bt(s,temp,index+1)#递归
res = []
bt(s,'',0)
return res









