5519. 重新排列單詞間的空格
傳送門
[傳送門]()
題意

結題思路
# 思路1:
# 周遊字元串,查詢其有n個空格,k個單詞,這些空格的數量要插在k-1個位置中。剩餘的餘數放在最末尾,傳回重新排列空格後的字元串。
# 總結:
# count():傳回字元串中某字元數量
# divmod():傳回商和餘數
# join():在一列list的每個元素後面連接配接前面的變量
class Solution(object):
def reorderSpaces(self, text):
"""
:type text: str
:rtype: str
"""
space_count = text.count(' ')
list1 = text.strip().split()
if len(list1) == 1:
return list1[0] + ' ' * space_count
k1, k2 = divmod(space_count, len(list1) - 1)
return (' ' * k1).join(list1) + ' ' * k2