LeetCode OJ 之 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 exactlyL 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.

给定一个字符串数组和一个长度L,格式化文本,使得每一行都有L个字符,并且是左右对齐的。

你应该以贪心的方式使用单词:就是说,每一行有尽可能多的单词,如果有必要可以增加空格' '。

单词之间额外的空格应该尽可能均匀分布。如果一行的空格不能均匀分布,左边的空位会被分配更多的空格。

对于最后一行,应该是左对齐,并且不能有额外的空格插入。

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.

注意:每个单词的长度都不超过L。(注意最后一行的最后也要用空格填满。)

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.
特殊情况:

除了最后一行,其他行都可能只包含一个单词。这种情况下,这一行应该是左对齐。

思路:

难点在于如何把多余的空格插入到前面的string中,这里采用的办法不是先构造前面的string,而是先保存当前行的单词,如果最后超过了,再构造整个一行,把空格插入到单词内。

代码:

class Solution {
public:
    vector<string> fullJustify(vector<string> &words, int L) 
    {
        vector<string> result;//保存结果
        vector<string> curLine;//保存当前行的单词,没有空格
        int curLen = -1;//当前行的长度,用来判断是否超过L
        int wordLen = 0;//当前行的所有单词总长度,用来求当前行的总的空格数
        for(int i = 0 ; i < words.size() ; i++)
        {
            if(curLen + 1 + words[i].size() > L)
            {
                result.push_back(CurLine(curLine , L , wordLen));//长度超过了,就构造当前行,通过在单词后面插入对应的空格数来构造
                curLine.clear();
                curLen = -1;
                wordLen = 0;
            }
            curLen += 1 + words[i].size();
            wordLen += words[i].size();
            curLine.push_back(words[i]);
        }
        result.push_back(LastLine(curLine , L , curLen));//构造最后一行,上面循环结束时,最后一行并没有求出,最后一行的构造方法不同于上面的
        return result;
    }
    string CurLine(vector<string> curLine , int L , int wordLen)
    {
        string curStr;
        if(curLine.size() == 1) //如果只有一个单词
        {
            curStr += curLine[0];
            for(int i = 0 ; i < L - wordLen ; i++)
            {
                curStr += ' ';
            }
            return curStr;
        }
        curStr = curLine[0];
        int count = curLine.size() - 1;
        int space = (L - wordLen) / count;//两个单词之间最少插入的空格数
        int extraSpa = (L - wordLen) % count;//多余的空格数均匀插在前面
        for(int i = 1 ; i < curLine.size() ; i++)
        {
            for(int j = 0 ; j < space ; j++ )
            {
                curStr += ' ';
            }
            if(extraSpa > 0)
            {
                curStr += ' ';
                extraSpa--;
            }
            curStr += curLine[i];
        }
        return curStr;
    }
    string LastLine(vector<string> curLine , int L , int curLen)
    {
        string curStr = curLine[0];
        //在每个单词后面加入一个空格
        for(int i = 1 ; i < curLine.size() ; i++)
        {
            curStr += " " + curLine[i];
        }
        //这个循环是在最后一行的最后一个单词后填满空格,题目要求的
        for(int i = 0 ; i < L - curLen ; i++)
        {
            curStr += ' ';
        }
        return curStr;
    }
    
};



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值