leetcode_77 Text Justification

161 篇文章 0 订阅

题目:

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

算法解析:

        首先要做的就是确定每一行能放下的单词数,就是比较n个单词的长度和加上n - 1个空格的长度跟给定的长度L来比较即可。

        找到了一行能放下的单词个数,然后计算出这一行存在的空格的个数,是用给定的长度L减去这一行所有单词的长度和。得到了空格的个数之后,就要在每个单词后面插入这些空格,这里有三种情况:

1)只有一个单词的,填充单词后,剩余长度填充空格

2)有两个及两个以上单词的,如果空格数不是单词间隔(单词数 - 1)的倍数,那么左边的空间里要比右边的空间里多加入一个空格,那么我们只需要用总的空格数除以空间个数,能除尽最好,说明能平均分配,除不尽的话就多加个空格放在左边的空间里,以此类推

3)如果是最后一行,除最后一个单词外,每个单词后面加上一个空格,所有单词填充完成后,剩余位置填充空格

class Solution {
public:
    vector<string> fullJustify(vector<string>& words, int maxWidth) {
        vector<string> res;
        if(words.size() == 0)   return res;
        vector<vector<string> > lineWords;
        vector<string> line;
        vector<int> lineLen;
        int len = words[0].length(); 
        line.push_back(words[0]);
        if(words.size() == 1)   lineWords.push_back(line);
        for(int i  = 1; i < words.size(); i++)
        {
            len += words[i].length() + 1;
            if(len <= maxWidth)
            {
                line.push_back(words[i]);
            }
            else
            {
                lineWords.push_back(line);
                line.clear();
                line.push_back(words[i]);
                lineLen.push_back(len - words[i].length() - 1);
                len = words[i].length();
            }
            if(i == (words.size() - 1))
            {
                lineWords.push_back(line);
                lineLen.push_back(len);
            }
        }
        for(int j = 0; j < lineWords.size();j++)
        {
            if(j < lineWords.size() - 1)
            {
                if(lineWords[j].size() == 1)
                {
                    string s = lineWords[j][0];
                    int rest = maxWidth - s.length();
                    s += string(rest,' ');
                    res.push_back(s);
                }
                else if(lineWords[j].size() >= 2)
                {
                    int n = lineWords[j].size();
                    int wlen = lineLen[j];
                    int lflen = maxWidth - wlen;
                    int spaceNum = n - 1;
                    int space = 1 + lflen/spaceNum;
                    int extralen = lflen - (space - 1)*spaceNum;
                    int extra = space + 1;
                    int k = 0;
                    string s = "";
                    while( k < n-1)
                    {
                        int sp;
                        if(k < extralen)
                            sp = extra;
                        else
                            sp = space;
                        s += lineWords[j][k++];
                        string ss = string(sp,' ');
                        s += ss;
                    }
                    s += lineWords[j][n - 1];
                    res.push_back(s);
                }
            }
            else
            {
                int wordcnt = lineWords[j].size();
                int  wt = 0;
                string s = "";
                while(wt < wordcnt){
                    s += lineWords[j][wt];
                    if(wt < wordcnt - 1)
                        s += " ";
                    wt++;
                }
                int rest = maxWidth - s.length();
                s += string(rest,' ');
                res.push_back(s);
            }
        }
        return res;
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值