力扣 784. 字母大小写全排列

解法:回溯

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def letterCasePermutation(s):
# 回溯
ret, cur = [], []
def fun(i):
if i >= s_num:
ret.append("".join(cur))
return
if s[i].isdigit():
cur.append(s[i])
fun(i + 1)
cur.pop()
elif 65 <= ord(s[i]) <= 90: # 大写字母
cur.append(s[i])
fun(i + 1)
cur.pop()
cur.append(chr(ord(s[i]) + 32))
fun(i + 1)
cur.pop()
else: # 小写字母
cur.append(s[i])
fun(i + 1)
cur.pop()
cur.append(chr(ord(s[i]) - 32))
fun(i + 1)
cur.pop()
s_num = len(s)
fun(0)
return ret
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022 eightyninth
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信