zoukankan      html  css  js  c++  java
  • 763划分字母区间

    from typing import List
    # 这道题使用双指针的方法写的。
    # 两个指针定义开头和结束,首先找到尾指针和头指针相同的字符。(注意要从后边寻找)
    # 然后判断两个指针中间的字符在字符串中是否存在其他的相同字符。
    class Solution:
    def partitionLabels(self, S: str) -> List[int]:
    # 定义列表,用来存放分段的字符串长度
    self.str_list = []
    # 字符串为空返回空列表
    if len(S) == 0:return []
    # 先定义两个指针
    index1,index2 = 0,len(S) - 1
    # 当寻找到分段字符串的长度等于字符串长度时就退出循环
    while sum(self.str_list) < len(S):
    # 首先找到尾指针和头指针相同的字符
    while S[index2] != S[index1]:
    index2 -= 1
    index = index1
    # 然后判断index1 - index2 中间的字符在S中的其他地方有没有出现
    while index1 != index2:
    # 如果有出现,就接着从后边遍历尾指针,找到那个和当前的字符相同的
    if S[index1] in S[index2:]:
    index2 = len(S) - 1
    while S[index2] != S[index1]:
    index2 -= 1
    index1 += 1
    # 将分段的字符串长度添加进去列表。
    self.str_list.append(index2 - index + 1)
    # 改变idnex1和index2的值,重新循环
    index1,index2 = index2 + 1,len(S) - 1
    return self.str_list


    A = Solution()
    print(A.partitionLabels("ababcbacadefegdehijhklij"))
    print(A.partitionLabels("aebbedaddc"))
  • 相关阅读:
    spring-boot整合shiro作权限认证
    spring boot整合redis,以及设置缓存过期时间
    java使用Http调用第三方接口,输入链接,获取返回值
    [C#]加密解密 MD5、AES
    [转]Linux 基本操作(RM 删除)
    [转]Windows 经验集
    [随手记]笔记+经验集
    [转][C#]Socket 手写服务端
    [转][C#]跳过调试
    [转][C#]抓取网页内容
  • 原文地址:https://www.cnblogs.com/cong12586/p/13463978.html
Copyright © 2011-2022 走看看