首页 > 其他分享 >leetcode 761

leetcode 761

时间:2023-02-05 08:55:25浏览次数:39  
标签:count index string self 761 list str leetcode

随便打开了一道题, 发现是hard, 然后发现自己居然会写...
`
class Solution:

def makeLargestSpecialSingle(self, s:str) -> str:
    # 最左的1和最右的0 相对应的字符串
    return "1" + self.makeLargestSpecial(s[1:-1]) + "0"

def makeLargestSpecial(self, s: str) -> str:
    # print(s)
    if s == "":
        return s
    string_list = []
    count = 0
    start_index = 0
    # 找到每一串可以独立成对的字符串
    for i, v in enumerate(s):
        if v == "1":
            count += 1
            continue
        elif v == "0":
            count -= 1
            if count == 0:
                string_list.append(s[start_index:i+1])
                start_index = i+1
    # print(string_list)
    string_list = [self.makeLargestSpecialSingle(i) for i in string_list]

    string_list.sort(reverse=True)
    # print(string_list)
    return "".join(string_list)

`

标签:count,index,string,self,761,list,str,leetcode
From: https://www.cnblogs.com/mangmangbiluo/p/17092839.html

相关文章