码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode] Text Justification

时间:2014-07-07 23:16:20      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   java   color   strong   

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/

 

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

 

bubuko.com,布布扣
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

 

[leetcode] Text Justification,布布扣,bubuko.com

[leetcode] Text Justification

标签:style   blog   http   java   color   strong   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!