"""
68. Text Justification
Description
HintsSubmissionsDiscussSolution
Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.
You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces " " when necessary so that each line has exactly L characters.
Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.
For the last line of text, it should be left justified and no extra space is inserted between words.
For example,
words: ["This", "is", "an", "example", "of", "text", "justification."]
L: 16.
Return the formatted lines as:
[
"This is an",
"example of text",
"justification. "
]
Note: Each word is guaranteed not to exceed L in length.
""" class Solution: def justify(self,words_list_in,maxWidth): lencur=len("".join(words_list_in)) len_spaces=maxWidth-lencur index=0 while len_spaces>0: if index>=len(words_list_in)-1: index=0 words_list_in[index]+=" " index+=1 len_spaces-=1 return "".join(words_list_in) def fullJustify(self, words, maxWidth): """ :type words: List[str] :type maxWidth: int :rtype: List[str] """ if maxWidth==0: return words strlist_out=[] strdict_cur={"list":list(),"len":0} index=0 while True: if index>len(words)-1: strlist_out.append(strdict_cur["list"]) # strdict_cur = {"list": list(), "len": 0} break elif strdict_cur["len"]<=maxWidth and strdict_cur["len"]+len(words[index])<=maxWidth: strdict_cur["list"].append(words[index]) strdict_cur["len"]+=(len(words[index])+1) index+=1 else: strlist_out.append(strdict_cur["list"]) strdict_cur = {"list": list(), "len": 0} # index+=1 newstr_list=[] for words_list in strlist_out: new_str=self.justify(words_list,maxWidth) newstr_list.append(new_str) newstr_list[-1]=" ".join(newstr_list[-1].split()) space_str=" "*(maxWidth-len(newstr_list[-1])) # print([space_str],maxWidth-len(newstr_list[-1]),len(newstr_list[-1]),newstr_list[-1]) last_str="".join([newstr_list[-1],space_str]) # print([last_str]) newstr_list[-1]=last_str # print(newstr_list) return newstr_list if __name__=="__main__": st=Solution() words=["This", "is", "an", "example", "of", "text", "justification."] L=16 # words=[""] # L=0 # words=["a"] # L=1 # words=["What","must","be","shall","be."] # L=12 st.fullJustify(words,L) """"""
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/41424.html
Problem Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified. You should pack your words in a greed...
摘要:探測器的代碼就是寫的,真厲害 New Horizon 探測器的代碼就是 JPL 寫的,真厲害 http://pixelscommander.com/wp-content/uploads/2014/12/P10.pdf Gerard J. Holzmann NASA/JPL Laboratory for Reliable Software Pasadena, CA 91109 Mo...
摘要:反向傳播提供了一個機器學習答案,然而就像下一段討論的那樣,它并非生物學上可信的。尋找一個生物學上可信的機器學習方法進行深度網絡中的信任分配是一個主要的長期問題,也是此論文貢獻的方向。 作者:Yoshua Bengio、Dong-Hyun Lee、Jorg Bornschein、Thomas Mesnard、Zhouhan Lin摘要神經科學家長期以來批評深度學習算法與當前的神經生物學知識彼此...
摘要:深入本屆集團公司黨委由公司黨委由本屆集團公司黨委由公司黨委由組均是,而組則是。下英文泰文等的默認對齊方式,下的默認對齊方式等同于,采用增加減少象形文字間的距離。 前言 也許提及text-align你會想起水平居中,但除了這個你對它還有多少了解呢?本篇打算和大家一起來跟text-align來一次負距離的交往,你準備好了嗎? text-align屬性詳解 The text-align C...
摘要:在這堂課中,學生將可以學習到深度學習的基礎,學會構建神經網絡,包括和等。課程中也會有很多實操項目,幫助學生更好地應用自己學到的深度學習技術,解決真實世界問題。 深度學習入門首推課程就是吳恩達的深度學習專項課程系列的 5 門課。該專項課程最大的特色就是內容全面、通俗易懂并配備了豐富的實戰項目。今天,給大家推薦一份關于該專項課程的核心筆記!這份筆記只能用兩個字形容:全面! showImg(...
閱讀 511·2021-10-09 09:44
閱讀 2073·2021-09-02 15:41
閱讀 3551·2019-08-30 15:53
閱讀 1829·2019-08-30 15:44
閱讀 1283·2019-08-30 13:10
閱讀 1188·2019-08-30 11:25
閱讀 1458·2019-08-30 10:51
閱讀 3365·2019-08-30 10:49