很烦的边界条件,还能输入[""],2这种数据的???然后标准输出是[" "],真搞不懂意义何在
class Solution(object):
def fullJustify(self, words, maxWidth):
"""
:type words: List[str]
:type maxWidth: int
:rtype: List[str]
"""
Len=len(words)
if Len==0:return []
ans=[]
if Len==1:
curans=""
curans+=words[0]
for i in range(maxWidth-len(words[0])):
curans+=" "
ans.append(curans)
return ans
curS=[words[0]]
curC=len(words[0])
for i in range(1,Len):
if curC+1+len(words[i])<=maxWidth:
curS.append(words[i])
curC+=1+len(words[i])
else:
ans.append(self.modify(curS,maxWidth))
curC=len(words[i])
curS=[]
curS.append(words[i])
if i==Len-1:
ans.append(self.modifyLast(curS,maxWidth))
return ans
def modify(self,s,l):
Len=len(s)
if Len==1:
ans=""
ans+=s[0]
for i in range(l-len(s[0])):
ans+=" "
return ans
gapNumber=Len-1
ans=""
totalLen=0
for i in range(Len):
totalLen+=len(s[i])
totalSpace=l-totalLen
MIN=totalSpace/gapNumber
bigSpaceNumber=totalSpace-MIN*gapNumber
smallSpaceNumber=gapNumber-bigSpaceNumber
ans+=s[0]
smallSpace=""
for i in range(MIN):
smallSpace+=" "
bigSpace=smallSpace+" "
cnt=1
while(bigSpaceNumber>0):
ans+=bigSpace
ans+=s[cnt]
cnt+=1
bigSpaceNumber-=1
while(smallSpaceNumber>0):
ans+=smallSpace
ans+=s[cnt]
cnt+=1
smallSpaceNumber-=1
return ans
def modifyLast(self,s,l):
ans=s[0]
for i in range(1,len(s)):
ans+=" "+s[i]
Len=len(ans)
for i in range(l-Len):
ans+=" "
return ans