[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 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.

https://oj.leetcode.com/problems/text-justification/

思路:字符串处理的题目,按要求依次处理各种情况即可。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution {
    public List<String> fullJustify(String[] words, int L) {
        List<String> res = new ArrayList<String>();
        if (words.length == 0) {
            return res;
        }
        if (L == 0) {//deal with L==0 and words are ""
            res = Arrays.asList(words);
            return res;
        }

        int len = words.length;
        int start = 0;
        int end = 0;
        int count = 0;

        while (start < len) {
            int i = start;
            while (i < len && count + words[i].length() <= L) {
                count += words[i].length() + 1;
                i++;
            }
            end = i - 1;

            int curWordsLen = count - (end - start + 1);

            if (end != len - 1)
                adjust(res, words, L, start, end, curWordsLen, false);
            else
                adjust(res, words, L, start, end, curWordsLen, true);
            start = end + 1;
            count = 0;
        }

        return res;
    }

    private void adjust(List<String> res, String[] words, int l, int start, int end, int wordsLen, boolean lastLine) {

        if (lastLine || end == start) {
            StringBuilder sb = new StringBuilder();
            for (int i = start; i <= end; i++) {
                sb.append(words[i]);
                if (i != end)
                    sb.append(" ");
            }
            while (sb.length() < l)
                sb.append(" ");

            res.add(sb.toString());

        } else {
            if (start != end) {
                int avgSpaces = (l - wordsLen) / (end - start);
                int extraSpace = (l - wordsLen) % (end - start);
                StringBuilder sb = new StringBuilder();
                for (int i = start; i <= end; i++) {
                    sb.append(words[i]);
                    if (i != end) {
                        for (int j = 0; j < avgSpaces; j++)
                            sb.append(" ");
                        if (extraSpace-- > 0)
                            sb.append(" ");
                    }
                }
                res.add(sb.toString());
            }

        }

    }

    public static void main(String[] args) {
        System.out.println(new Solution().fullJustify(new String[] { "This", "is", "an", "example", "of", "text",
                "justification." }, 16));
        System.out.println(new Solution().fullJustify(new String[] { "" }, 0));

    }
}
View Code

第二遍记录:太烦懒得做了。。 

参考:

http://blog.csdn.net/linhuanmars/article/details/24063271

http://www.cnblogs.com/TenosDoIt/p/3475275.html

原文地址:https://www.cnblogs.com/jdflyfly/p/3812744.html