leetcode 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 maxWidthcharacters.

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

给定一个单词数组和一个长度 maxWidth,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本。

你应该使用“贪心算法”来放置给定的单词;也就是说,尽可能多地往每行中放置单词。必要时可用空格 ' ' 填充,使得每行恰好有 maxWidth 个字符。

要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数。

文本的最后一行应为左对齐,且单词之间不插入额外的空格。

说明:

单词是指由非空格字符组成的字符序列。
每个单词的长度大于 0,小于等于 maxWidth。
输入单词数组 words 至少包含一个单词。
示例:

输入:
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
输出:
[
   "This    is    an",
   "example  of text",
   "justification.  "
]
示例 2:

输入:
words = ["What","must","be","acknowledgment","shall","be"]
maxWidth = 16
输出:
[
  "What   must   be",
  "acknowledgment  ",
  "shall be        "
]
解释: 注意最后一行的格式应为 "shall be    " 而不是 "shall     be",
     因为最后一行应为左对齐,而不是左右两端对齐。       
     第二行同样为左对齐,这是因为这行只包含一个单词。
示例 3:

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

解题思路:

用一个vector<string> q存每行要存的字符串,遍历words中的每个字符串,用cnt记录当前行中的字符长度。

cnt + word.size() < maxWidth

因为字符串之间一定有一个空格隔开,就将word加一个空格后加进q中,更新cnt

word += ' ' ;
q.push_back(word) ;
cnt += word.size() ;
else if(cnt + word.size() == maxWidth)

此时的word就是当前行的最后一个字符串;

word += ' ' ;
q.push_back(word) ;
cnt += word.size() ;
string s = chan_to_str(cnt , maxWidth , q) ;
ans.push_back(s) ;
q.clear() ;
cnt = 0 ;

之所以还将word加空格后加进q是为了和后面大于maxwidth这种情况统一处理。因为如果cnt+word.size() > maxwidth,那么q中的最后一个字符串最后一个字符肯定是空格。如此做法是为了方便chan_to_str这个函数统一处理。将q中的所有字符串连接起来后,要清空q,并将cnt置0,重新开始下一行;

            else
            {
                --i ;
                string s = chan_to_str(cnt , maxWidth , q) ;
                ans.push_back(s) ;
                q.clear() ;
                cnt = 0 ;
            }

将i--,是当前word并没有加入当前行。

这里要特别关注的是最后一行,因为最后一行字符串间没有额外的空格。

如果最后一行最后一个字符串满足这种性质,就不用额外考虑。

else if(cnt + word.size() == maxWidth)

如果不满足,说明最后一个字符串加入q时,cnt<maxwidth。可以通过判断q是否非空来判断是否是最后一行。

        if(!q.empty())
        {
            string s ;
            for(int i = 0 ; i < q.size() ; ++i)
            {
                s += q[i];
            }
            s += string(maxWidth - s.size() , ' ') ;
            ans.push_back(s) ;
        }

 将q中所有字符串连接:

    string chan_to_str(int cnt , int maxWidth , vector<string>& q)
    {
        if(q.empty()) return "" ;
        string word = q.back() ;
        q.pop_back() ;
        int len = word.size() ;
        word = word.substr(0 , len - 1) ;  //去掉最后一个单词的后缀空格 。
        cnt -= 1 ;
        q.push_back(word) ;
        string ans ;
        
        if(q.size() == 1)
        {
            int len = q[0].size() ;
            ans = q[0] + string(maxWidth - len , ' ') ;
            return ans ;
        }
        else
        {
            int quo = (maxWidth - cnt) / (q.size() - 1) ;
            int mo = (maxWidth - cnt) % (q.size() - 1) ;

            for(int i = 0 ; i < q.size() - 1 ; ++i)
            {
                ans += q[i] + string(quo , ' ') ;
                if(i < mo) ans += string(1 , ' ') ;
            }
            ans += q.back() ;
        }
        
        return ans ;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值