String - 68. Text Justification

2020-09-23 17:19:01 浏览数 (2)

68. Text Justification

Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth 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 maxWidth 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.

Note:

  • A word is defined as a character sequence consisting of non-space characters only.
  • Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
  • The input array words contains at least one word.

Example 1:

Input: words = ["This", "is", "an", "example", "of", "text", "justification."] maxWidth = 16 Output: [    "This    is    an",    "example  of text",    "justification.  " ]

思路:

也是一个实现题,没有什么算法,就是找到每一行能放下的单词,然后按照case的规则拼接输出结果。

代码:

java:

代码语言:javascript复制
public class Solution {

    public List<String> fullJustify(String[] words, int maxWidth) {
        List<String> lines = new ArrayList<String>();
        int index = 0;
        while (index < words.length) {
            //count:该行所有单词累计总长度
            int count = words[index].length();
            //last:该行最后一个词的index
            int last = index   1;
            while (last < words.length) {
                //out of bound
                if (words[last].length()   count   1 > maxWidth) break;
                //plus one for the space, if its a perfect fit it will fit
                count  = 1   words[last].length();
                last  ;
            }
            StringBuilder builder = new StringBuilder();
            //append该行第一个单词
            builder.append(words[index]);
            //这一行除去第一个已经append的单词,共剩下几个词语:diff 个:从index到last-1
            int diff = last - index - 1;
           // if last line or number of words in the line is 1, left-justified
            //最后一行:每个单词中间一个空格, 剩余补上空白
            if (last == words.length || diff == 0) {
                for (int i = index 1; i < last; i  ) {
                    builder.append(" ");
                    builder.append(words[i]);
                }
                for (int i = builder.length(); i < maxWidth; i  ) {
                    builder.append(" ");
                }
            } else {
                //不是最后一行:middle justified
                //这一行总space的个数:(长度-累计单词总长度)
                //每个单词后面space的个数:(长度-累计单词总长度)/单词个数
                // r为需要平均分配到中间的空格总数
                int spaces = (maxWidth - count) / diff;
                int r = (maxWidth - count) % diff;
                for (int i = index 1; i < last; i  ) {
                    for(int k=spaces; k > 0; k--) {
                        builder.append(" ");
                    }
                    if(r > 0) {
                        builder.append(" ");
                        r--;
                    }
                    builder.append(" ");
                    builder.append(words[i]);
                }
            }
            lines.add(builder.toString());
            index = last;
        }
        return lines;
    }
}

0 人点赞