"""
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) """"""
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/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(...
阅读 476·2021-10-09 09:44
阅读 2015·2021-09-02 15:41
阅读 3520·2019-08-30 15:53
阅读 1786·2019-08-30 15:44
阅读 1245·2019-08-30 13:10
阅读 1140·2019-08-30 11:25
阅读 1373·2019-08-30 10:51
阅读 3315·2019-08-30 10:49