leetcode 761
随便打开了一道题, 发现是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)
`