Text Justification

来源:互联网 发布:华为网络机顶盒插上u盘 编辑:程序博客网 时间:2024/05/22 14:29

Given an array of words and a length L, format the text such that each line has exactlyL characters and is fully (left and right) justified.

You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces' ' when necessary so that 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 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 should 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 as:

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

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

public class Solution {    public ArrayList<String> fullJustify(String[] words, int L) {// Start typing your Java solution below// DO NOT write main() functionArrayList<String> res = new ArrayList<String>();if (words.length == 0)return res;int index = 0;StringBuilder sb = new StringBuilder();while (index < words.length) {sb = new StringBuilder();int volume = 0; // number of chars this lineint cur_len = 0;// number of chars plus one space for each wordint counter = 0;int initial = index;while (index < words.length && words[index].length() + cur_len <= L) {cur_len += (words[index].length() + 1);// at least one spacevolume += words[index].length();counter++;index++;}if(index == words.length){// if last line, all words go leftwhile(initial < index){sb.append(words[initial++]);if(sb.length() < L)sb.append(" ");// one space per word, if length permits for the last word}while(sb.length() < L)sb.append(" ");// add spaces till end of last lineres.add(sb.toString());break;}int spaces = L - volume;if (counter != 1) {int spaces_per_word = spaces / (counter - 1);int extra_spaces = spaces % (counter - 1);while(initial < index){// no "=", since index points to the next word nowsb.append(words[initial++]);if(--counter == 0)//the last word does not need spacebreak;if(extra_spaces > 0){for(int i = 0; i < spaces_per_word; i++)sb.append(" ");sb.append(" ");// extra one apace for each wordextra_spaces--;}else{for(int i = 0; i < spaces_per_word; i++)sb.append(" ");}}}else{// if only one word this line, all spaces go rightsb.append(words[initial]);for(int i = 0; i < spaces; i++)sb.append(" ");}res.add(sb.toString());}return res;}}


原创粉丝点击