栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

字串和子序列(python)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

字串和子序列(python)

字符串的字串,就是和顺序相关的。
子序列和顺序没有关系,对于一个字符来说可以要也可以不要。

  • 字串的实现
class Substring(object):
    """字串"""
    def substring(self, str):
        length = len(str)
        res = []
        for i in range(length):
            string = ""
            for j in range(i, length):
                string += str[j]
                res.append(string)
        return res
  • 子序列的代码实现
class Subsequence(object):

    def substring(self, string):
        """子序列"""
        res = set()

        def sub(string, index, path):
            if index == length:
                if path:
                    res.add(path)
                return res
            sub(string, index + 1, path)  # 不要当前位置的字符
            yes = path + string[index] # 要当前位置的字符
            sub(string, index + 1, yes)

        sub(string, 0, "")
        return res
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/360307.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号