Re: [闲聊] 每日leetcode

楼主: DJYOMIYAHINA (通通打死)   2024-12-03 21:47:34
还蛮男的
不过有过就好
写的好像也不是很好
def addSpaces(self, s: str, spaces: List[int]) -> str:
ans = ""
cur_i = 0
for i,c in enumerate(s):
if cur_i<len(spaces) and i==spaces[cur_i]:
ans = ans + f" {c}"
cur_i += 1
else:
ans = ans + c
return ans
一开始写这样TLE 笑了
def addSpaces(self, s: str, spaces: List[int]) -> str:
cur_cnt = 0
for num in spaces:
s = s[:num+cur_cnt] + " " + s[num+cur_cnt:]
cur_cnt += 1
return s

Links booklink

Contact Us: admin [ a t ] ucptt.com