# Leetcode 68. Text justification Textual adjustment Problem solving report

Source: Internet
Author: User

1 Thinking of solving problems

This problem, in fact, I do not know why to mark as hard mode, the main idea of the topic is to format a string array to adjust, output the corresponding sentence.
Requirements are:
1. The string length of each line cannot exceed one fixed length maxwidth
2, every two words must have a space, if the line between the words can not be broken down, then must be left more space, less on the right. And, the space more places than the right one less than a
3, the last line does not use 2 of the space method, the normal each word empty one box is good, finally stay white is good

Put an explanation in advance:
* The key to this problem is to carefully handle each step:
* 1, each line to choose K words, K words length +k-1 space must be less than maxwidth, here each time you choose to meet the maximum value of this condition can be
* 2, for already selected K words, first calculate basic space, that is space= (maxwidth-all words of length)/(k-1), but there is a part of the extra space, then in the additional space, from the left to start each time to add one, The left space to satisfy the topic is greater than or equal to the right (at most one)
* 3, note only 1 words of the scene
* 4, the last line needs to be adjusted, the last line of words between the space is only 1, the end of a space to complement the length "

2 Original Questions

Given an array of words and a length L, format the text such, all line have exactly L characters and is fully D right) justified.

You should pack your words in a greedy approach; That's, pack as many words as you can on each line. Pad extra spaces ' when necessary so, 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 does not divide evenly between words, the empty slots on the left would be assigned more SP Aces than the slots on the right.

For the last line of text, it should being justified and no extra space is inserted between words.

For example,
Words: ["This", "was", "an", "example", "of", "text", "justification."]
L:16.

Return the formatted lines as:
[
"This was an",
"Example of text",
"Justification."
]

3 AC Solution
`` Public  class solution {    /** * The key is to carefully handle each step: * 1, each line to choose the word k, the length of the K word +k-1 the length of the space must be less than maxwidth, where each choice to meet the maximum value of this condition can be * 2, for already selected K words, First calculate the basic space, that is, space= (the length of all words maxwidth-)/(k-1), but there are some extra space, then in the additional space, from the left to start each time to add one, to meet the title of the left side of the space greater than or equal to the right (at most one) * 3, Note that there are only 1 words in the scene * 4, the last line needs to be adjusted, the last line of words between the space is only 1, the end of a space to complement the length "* * * *     PublicList<string>fulljustify(string[] words,intMaxWidth) {list<string> result =NewArraylist<string> ();intstart=0, end=1, N=words.length; while(start<n) {intcompulsoryspaces=0;//Required space, number of words currently selected-1            intWordlength=words[start].length ();//number of current words             while(End<n && compulsoryspaces+1+wordlength+words[end].length () <=maxwidth) {//Test Select the maximum number of wordscompulsoryspaces++;                Wordlength+=words[end].length ();            end++; }if(end==n) {//end-of-line special treatmentStringBuilder sb=NewStringBuilder (Words[start]); for(intk=start+1; k<end;k++) Sb.append (" "+words[k]); for(intk=wordlength+compulsoryspaces;k<maxwidth;k++) Sb.append (" "); Result.add (Sb.tostring ()); Break; }if(end-start==1){//Select only one of the special handling, because the calculation space does not appear divisor to 0 conditionStringBuilder sb=NewStringBuilder (Words[start]); for(intk=wordlength;k<maxwidth;k++) Sb.append (" ");            Result.add (Sb.tostring ()); }Else{//Processing multiple spaces                intSpace = (maxwidth-wordlength)/(end-start-1);//Basic spaces                intremains = maxwidth-wordlength-(end-start-1) *space;//Because the number of spaces divisible by the failed allocationStringBuilder sb=NewStringBuilder (Words[start]); for(intk=start+1; k<end;k++) { for(intL=0; l<space;l++) Sb.append (" ");if(remains-->0) Sb.append (" ");//At more than 0, that is also need to add more space on the left, give aSb.append (Words[k]);            } result.add (Sb.tostring ());            } start=end; End=end+1; }returnResult }}``

Leetcode 68. Text justification Textual adjustment Problem solving report

Related Keywords:

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