Text Justification

来源:互联网 发布:warframe下载数据损坏 编辑:程序博客网 时间:2024/05/19 02:04

Q:

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 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 exactlyL 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."]
L16.

Return the formatted lines as:

[   "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 should you do in this case?
    In this case, that line should be left-justified.

Solution:

public class Solution {    public List<String> fullJustify(String[] words, int L) {        List<String> result = new ArrayList<String>();        if (words == null || words.length == 0)            return result;                int i = 0;        while (i < words.length) {            StringBuilder sb = new StringBuilder();            int remain = L;            sb.append(words[i]);            remain = remain - words[i].length();            if (i == words.length-1) {                for (int k = remain; k > 0; k--)                    sb.append(' ');                result.add(sb.toString());                break;            }            int j = i+1;            while (j < words.length) {                if (remain >= words[j].length() + 1) {                    remain = remain - words[j].length() - 1;                    j++;                }                else                    break;            }            if (j == i+1) {                for (int k = remain; k > 0; k--)                    sb.append(' ');                result.add(sb.toString());                i = j;                continue;            }            else {                if (j == words.length) {                    for (int p = i+1; p < j; p++) {                        sb.append(' ');                        sb.append(words[p]);                    }                    for (int q = remain; q > 0; q--)                        sb.append(' ');                }                else {                    int avg = remain / (j - i - 1);                    int morespace = remain % (j - i - 1);                    for (int p = i+1; p < j; p++) {                        sb.append(' ');                        if (morespace > 0) {                            sb.append(' ');                            morespace--;                        }                        for (int k = 0; k < avg; k++)                            sb.append(' ');                        sb.append(words[p]);                    }                }            }            result.add(sb.toString());            i = j;        }        return result;    }}


0 0
原创粉丝点击