68-对文本格式化

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

问题描述

给定字符串数组words和maxWidth, 对文本进行格式化使得每行正好有maxWidth个字符

你应该以贪心方式装填字符串, 每行尽可能地放字符串。有必要的时候, 填充” “, 使得每行有maxWidth个字符

字符之间的额外空间应该尽量均匀。如果一行中的空格分布不均匀, 左边会比右边分配更多的空格

对于最后一行文本, 应该左正当, 并且字符串之间没有插入额外的空格


解法

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();
    }
}

猜你喜欢

转载自blog.csdn.net/LaputaFallen/article/details/80056709
今日推荐