"Leetcode" Text Justification Problem Solving report

Source: Internet
Author: User

Topic

Given an array of words and a length L, format the text such, all line have exactly L characters and is fully (left and 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 ‘ ‘ exactlyL 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 the text, it should was 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    was an",   "example  of text", "   justification.  "]

Note: Each word was guaranteed not to exceed L in length.

Click to show corner cases.

Corner Cases:

    • A line and than the last line might contain only one word. What should?
      In this case, then line should is left-justified.
Resolution

Test instructions: Put a set of words in each line of L Word descriptors, each line to justify, if the space can not be evenly distributed in all intervals, then the left of the space is more than the right space, the last line is left-aligned.

Idea: There is no special algorithm, is the simulation, the main sub-case judgment. First divided into two categories, the last line and non-final line, and then the end of all the words in a space, the final side of the supplementary space, non-final line and then divided into two categories, if there is only one word on the left, the right to fill the space, if there are more than one word, That is, there are several interval num and a few extra spaces extra (except for each of the two words an empty extra), each interval again extra/num, the first extra%num interval to put more space.

"Java Code"

public class Solution {public list<string> fulljustify (string[] words, int L) {list<string> ans =                New Arraylist<string> ();        int n = words.length;        int i = 0;            while (I < n) {int len = words[i].length ();            Int J = i + 1;                while (J < n && len + 1 + words[j].length () <= L) {len + = 1 + words[j].length ();            j + +;            } String line = Words[i];  if (j = = N) {//If this was the last line for (int k = i + 1, k < n; k++) {line + = "                "+ words[k];                } while (Line.length () < L) {line + = ";                }} else {int extrawhite = L-len;                                int whitenum = j-i-1;         if (Whitenum = = 0) {//If this line had only one word while (Line.length () < L) {           Line + = "";                    }} else {for (int k = i + 1, K < J; k++) {line + = "";                    for (int p = 0, p < extrawhite/whitenum; p++) {line + = "";                    } if (K-i <= extrawhite%whitenum) {line + = "";                } line + = Words[k];                        }}} ans.add (line);        i = j;    } return ans; }}


"Leetcode" Text Justification Problem Solving report

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.