68 - Formatting text

Description

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

Example 2:

Input:
words = ["What","must","be","acknowledgment","shall","be"]
maxWidth = 16
Output:
[
  "What   must   be",
  "acknowledgment  ",
  "shall be        "
]
Explanation: Note that the last line is "shall be    " instead of "shall     be",
             because the last line must be left-justified instead of fully-justified.
             Note that the second line is also left-justified becase it contains only one word.

Example 3:

Input:
words = ["Science","is","what","we","understand","well","enough","to","explain",
         "to","a","computer.","Art","is","everything","else","we","do"]
maxWidth = 20
Output:
[
  "Science  is  what we",
  "understand      well",
  "enough to explain to",
  "a  computer.  Art is",
  "everything  else  we",
  "do                  "
]

Problem Description

Given a string array words and maxWidth, format the text so that each line has exactly maxWidth characters

You should fill strings greedily, putting as many strings as possible on each line. When necessary, pad with " " so that each line has maxWidth characters

The extra space between characters should be as even as possible. If spaces in a line are not evenly distributed, more spaces are allocated to the left than to the right

For the last line of text, it should be left justified and no extra spaces are inserted between strings


solution

class Solution {
    public List<String> fullJustify(String[] words, int maxWidth) {
        int len = words.length;
        List<String> res = new ArrayList();

        int i = 0;
        while(i < len){
            int width = 0;
            int cnt = 0;
            //startidx为字符串开始位置
            int startidx = i;
            //获取尽可能多的字符串
            while(i < len){
                width += words[i].length();
                cnt++;
                if(width + cnt - 1 <= maxWidth){
                    i++;
                }else{
                    width -= words[i].length();
                    cnt--;
                    break;
                }
            }
            //endidx为字符串结束位置
            int endidx = i - 1;
            //将获取的字符串通过formline()形成一行
            //cnt为字符串个数, width为总的宽度, maxWidth - width为需要填充的空格数
            res.add(formLine(words, startidx, endidx, width, cnt, maxWidth - width));
        }

        return res;
    }
    public String formLine(String[] words, int startidx, int endidx, int width, int cnt, int spacecnt){
        StringBuilder strb = new StringBuilder();
        //如果只有一个字符串, 直接在后面填充空格返回
        if(cnt == 1){
            strb.append(words[startidx]);
            for(int i = 0;i < spacecnt;i++) strb.append(" ");
            return strb.toString();
        }
        //填充均匀, x为单词间填充空格数
        //y为余数, 当无法填充均匀时,从左到右一次加1个空格
        int x = spacecnt / (cnt - 1);
        int y = spacecnt % (cnt - 1);
        for(int i = startidx;i <= endidx;i++){
            strb.append(words[i]);
            //若为最后一行, 需要左正当
            if(endidx == words.length - 1){
                if(spacecnt > 0)   strb.append(" ");
                spacecnt--;
                if(i == endidx){
                    for(int j = 0;j < spacecnt;j++) strb.append(" ");
                }
            }else{
                if(i != endidx){
                    for(int j = 0;j < x;j++)    strb.append(" ");
                    if(y > 0)  strb.append(" ");
                    y--;
                }
            }
        }

        return strb.toString();
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324723566&siteId=291194637