成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專(zhuān)欄INFORMATION COLUMN

leetcode-68-Text Justification

remcarpediem / 1216人閱讀

"""
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)
    """"""

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/41424.html

相關(guān)文章

  • [LeetCode] 68. Text Justification

    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...

    Karrdy 評(píng)論0 收藏0
  • The Power of Ten – Rules for Developing Safety Cri

    摘要:探測(cè)器的代碼就是寫(xiě)的,真厲害 New Horizon 探測(cè)器的代碼就是 JPL 寫(xiě)的,真厲害 http://pixelscommander.com/wp-content/uploads/2014/12/P10.pdf Gerard J. Holzmann NASA/JPL Laboratory for Reliable Software Pasadena, CA 91109 Mo...

    Muninn 評(píng)論0 收藏0
  • Yoshua Bengio最新修改版論文:邁向生物學(xué)上可信的深度學(xué)習(xí)

    摘要:反向傳播提供了一個(gè)機(jī)器學(xué)習(xí)答案,然而就像下一段討論的那樣,它并非生物學(xué)上可信的。尋找一個(gè)生物學(xué)上可信的機(jī)器學(xué)習(xí)方法進(jìn)行深度網(wǎng)絡(luò)中的信任分配是一個(gè)主要的長(zhǎng)期問(wèn)題,也是此論文貢獻(xiàn)的方向。 作者:Yoshua Bengio、Dong-Hyun Lee、Jorg Bornschein、Thomas Mesnard、Zhouhan Lin摘要神經(jīng)科學(xué)家長(zhǎng)期以來(lái)批評(píng)深度學(xué)習(xí)算法與當(dāng)前的神經(jīng)生物學(xué)知識(shí)彼此...

    xingpingz 評(píng)論0 收藏0
  • CSS魔法堂:你真的懂text-align嗎?

    摘要:深入本屆集團(tuán)公司黨委由公司黨委由本屆集團(tuán)公司黨委由公司黨委由組均是,而組則是。下英文泰文等的默認(rèn)對(duì)齊方式,下的默認(rèn)對(duì)齊方式等同于,采用增加減少象形文字間的距離。 前言 也許提及text-align你會(huì)想起水平居中,但除了這個(gè)你對(duì)它還有多少了解呢?本篇打算和大家一起來(lái)跟text-align來(lái)一次負(fù)距離的交往,你準(zhǔn)備好了嗎? text-align屬性詳解 The text-align C...

    tinylcy 評(píng)論0 收藏0
  • 《DeepLearning.ai 深度學(xué)習(xí)筆記》發(fā)布,黃海廣博士整理

    摘要:在這堂課中,學(xué)生將可以學(xué)習(xí)到深度學(xué)習(xí)的基礎(chǔ),學(xué)會(huì)構(gòu)建神經(jīng)網(wǎng)絡(luò),包括和等。課程中也會(huì)有很多實(shí)操項(xiàng)目,幫助學(xué)生更好地應(yīng)用自己學(xué)到的深度學(xué)習(xí)技術(shù),解決真實(shí)世界問(wèn)題。 深度學(xué)習(xí)入門(mén)首推課程就是吳恩達(dá)的深度學(xué)習(xí)專(zhuān)項(xiàng)課程系列的 5 門(mén)課。該專(zhuān)項(xiàng)課程最大的特色就是內(nèi)容全面、通俗易懂并配備了豐富的實(shí)戰(zhàn)項(xiàng)目。今天,給大家推薦一份關(guān)于該專(zhuān)項(xiàng)課程的核心筆記!這份筆記只能用兩個(gè)字形容:全面! showImg(...

    wenhai.he 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<