Text Justification--Leetcode

Source: Internet
Author: User

original title Link:http://oj.leetcode.com/problems/text-justification/ 
This problem is purely string manipulation. To arrange a string of words into multiple-line-delimited strings. The main difficulty is the arrangement of spaces, first of all the words must be separated by a space. And when the current line does not fit many other words and the characters do not fill the length L. We're going to fill the space evenly between the words. Assuming that the remaining amount of space is just the interval multiplier then evenly distribute it. Otherwise, you must put more than one space in the preceding interval. Implementation we maintain a count count record for the current length. After that we calculate the amount of space that is common and the amount of space that is more than one, and then construct it as a line string. The last detail is that the last line does not need to evenly allocate spaces. The end of the sentence can be left blank. So take care of it alone. Time we need to scan the word again, and then at the end of the line to scan the current line of words, but the whole word will not be interviewed more than two times, so the overall time complexity is O (n). Spatial complexity is the size of the result (in relation to the number and length of words, which cannot be defined accurately, assuming that the last number of rows R is known.) Is O (r*l)). Code such as the following:  
Public arraylist<string> fulljustify (string[] words, int L) {arraylist<string> res = new Arraylist<stri    Ng> ();    if (Words==null | | words.length==0) return res;    int count = 0;    int last = 0; for (int i=0;i<words.length;i++) {if (Count+words[i].length () + (I-last) >l) {int Spacenum            = 0;            int extranum = 0;                if (i-last-1>0) {spacenum = (l-count)/(I-LAST-1);            Extranum = (l-count)% (i-last-1);            } StringBuilder str = new StringBuilder ();                for (int j=last;j<i;j++) {str.append (words[j]);                        if (j<i-1) {for (int k=0;k<spacenum;k++) {                    Str.append ("");                    } if (extranum>0) {str.append (""); } extranum--;                }} for (int j=str.length (); j<l;j++) {Str.append ("");            } res.add (Str.tostring ());            count=0;        last=i;    } count + = Words[i].length ();    } StringBuilder str = new StringBuilder ();        for (int i=last;i<words.length;i++) {str.append (words[i]);    if (Str.length () <l) str.append ("");    } for (int i=str.length (); i<l;i++) {Str.append ("");    } res.add (Str.tostring ()); return res;}
This problem belongs to the kind of text editing sub-operation and other topics, from the algorithm thinking of nothing special. But still quite a lot of implementation details, don't easy in the first time, you may want to practice a few times ha.

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Text Justification--Leetcode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.