[leetcode] 68. Text Justification

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

For example,
words["This", "is", "an", "example", "of", "text", "justification."]
L16.

Return the formatted lines as:

[
   "This    is    an",
   "example  of text",
   "justification.  "
]

Note: Each word is guaranteed not to exceed L in length.

Corner Cases:

  • A line other than the last line might contain only one word. What should you do in this case?
    In this case, that line should be left-justified.

这道题是实现文本编辑器的简单对齐功能,题目难度为Hard。


题目和算法不沾边,都是些字符串操作,比较繁琐,简单说一下处理过程。如果遍历到当前单词本行长度没有超出maxWidth,将当前单词加入本行,继续查看下一单词,判断依据是用这一行的单词总长度加上当前单词长度再加上空格数(单词之间预留一个空格)和maxWidth比较,如果超出了maxWidth,表明之前的单词构成了一行,就可以处理这些单词了,之后结束本行,从当前单词开始新的一行。在单词之间插空格的方法就不详细说了,比较简单,这里需要注意的是题目中提到的每行只有一个单词的情况,需要特殊处理,考虑时要考虑到这种情况。遍历完所有单词之后需要特殊处理最后一行,这一行是居左对齐的,每个单词之间只有一个空格,处理完这行的单词之后,在后面补齐空格就可以了。具体代码:

class Solution {
public:
    vector<string> fullJustify(vector<string>& words, int maxWidth) {
        int sz = words.size();
        int len = 0, idx = 0, bgn = 0;
        vector<string> ret;
        
        while(idx < sz) {
            if(len+words[idx].size()+idx-bgn <= maxWidth) {
                len += words[idx].size();
                idx++;
            }
            else {
                string line = "";
                int spaceCnt = (idx-bgn>1) ? idx-bgn-1 : 1;
                int spaceLen = (maxWidth-len)/spaceCnt;
                for(int i=0; i<idx-bgn; ++i) {
                    line += words[bgn+i];
                    if(idx-bgn != 1 && i == idx-bgn-1) break;
                    if(i < maxWidth-len-spaceLen*spaceCnt)
                        line += string(spaceLen+1, ' ');
                    else
                        line += string(spaceLen, ' ');
                }
                ret.push_back(line);
                
                len = 0;
                bgn = idx;
            }
        }
        
        string line = "";
        for(int i=bgn; i<idx; ++i) {
            line += words[i];
            if(i == idx-1) break;
            line += " ";
        }
        line += string(maxWidth-len-(idx-bgn-1), ' ');
        ret.push_back(line);
        
        return ret;
    }
};

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值