[LeetCode] Text Justification

Source: Internet
Author: User

[LeetCode] Text Justification

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 shoshould pack your words in a greedy approach; that is, pack as your words as you can in each line. Pad extra spaces' 'When necessary so that each line has exactlyL characters.

Extra spaces between words shoshould 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 shocould 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:

[   "This    is    an",   "example  of text",   "justification.  "]

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

Click to show corner cases.

Corner Cases:

  • A line other than the last line might contain only one word. What shoshould you do in this case?
    In this case, that line shoshould be left-justified. Pay attention to the last corner case.

    public class Solution {    public List
       
         fullJustify(String[] words, int L) {        List
        
          res = new ArrayList<>();        List
         
           list = new ArrayList<>();        int i=0,len = 0,strLen = 0;        while(i
          
            list,int L,int strLen){    if(list.size()==1){    StringBuilder sb = new StringBuilder();    for(int i=0;i
           
            



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.