zoukankan      html  css  js  c++  java
  • 784. 字母大小写全排列



    原串里有数字、也有字母,所以每选一个字符要加以判断,要是字母则有两种选择,数字就一种。

    还是套模板,模板可参考:https://www.cnblogs.com/panweiwei/p/14025143.html

    class Solution(object):
        def __init__(self):
            self.res = []
    
        def letterCasePermutation(self, S):
            """
            :type S: str
            :rtype: List[str]
            """
            # 特判
            if not S:
                return []
            # 将字符串转成list类型
            lists = list(S)
            # 去原串长度
            n = len(lists)
            self.dfs(lists, 0, n, [])
            return self.res
    
        def dfs(self, string, cur, n, temp):
            # 定义递归出口
            if len(temp) == len(string):
                self.res.append("".join(temp))
                return
            # 数字直接加
            if string[cur].isdigit():
                self.dfs(string, cur + 1, n, temp + [string[cur]])
            # 小写字母
            elif string[cur].islower():
                self.dfs(string, cur + 1, n, temp + [string[cur]])
                self.dfs(string, cur + 1, n, temp + [string[cur].upper()])
            # 大写字母
            elif string[cur].isupper():
                self.dfs(string, cur + 1, n, temp + [string[cur]])
                self.dfs(string, cur + 1, n, temp + [string[cur].lower()])
    
  • 相关阅读:
    day14
    day13
    装饰器小题
    day12
    tes..
    1380 没有上司的舞会
    算法模板——KMP字符串匹配
    算法模板——Tarjan强连通分量
    3211: 花神游历各国
    1131: [POI2008]Sta
  • 原文地址:https://www.cnblogs.com/panweiwei/p/14025298.html
Copyright © 2011-2022 走看看