[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