68. Text Justification

problem:

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

本问题这道题的要求是给定一组单词和长度L,以两端对齐的方式格式化文本使每行只有L个字符,其中最后一行左对齐。每行的单词间可能需要补充额外的空格,如果空格数目不能整除单词间隔的数目,则左侧数目要多于右侧。

a、当前行只放一个单词时,空格全部在这个单词的右边 

b、最后一行,单词间只有一个空格,其余空格全部在右边。然后只要贪心选择,在一行中尽可能放更多的单词。

class Solution {
public:
    vector<string> fullJustify(vector<string>& words, int maxWidth) {
        vector<string> result;
        int wordslength = words.size();
        int i,k;
        for(i=0; i<wordslength; i++)
        {
            string s = words[i];
            for(k = i+1; k<wordslength; k++)
            {
                string tmp = s + words[k];
                if(tmp.size()+k-i > maxWidth)
                    break;
                s += words[k];
            }

            int spaces = maxWidth - s.size();
            int cnt = k-i;
            if(cnt == 1)
            {
                string tmp(spaces, ' ');
                s += tmp;
            }
            else
            {
                int p = i;
                int sum = 0;
                if(k != wordslength)
                {
                    while(spaces)
                    {
                        int num = (int)ceil(spaces/(double)(cnt-1));
                        spaces -= num;
                        sum += words[p].size();
                        string tmp(num, ' ');
                        s = s.substr(0, sum) + tmp + s.substr(sum);
                        sum += num;
                        p++;
                        cnt--;
                    }
                }
                else
                {
                    int remain = cnt - 1;
                    while(remain)
                    {
                        spaces--;
                        sum += words[p].size();
                        s = s.substr(0,sum)+" "+s.substr(sum);
                        sum+=1;
                        p++;
                        remain--;
                    }
                    string tmp(spaces, ' ');
                    s += tmp;
                }
            }
            result.push_back(s);
            i = k-1;
        }
        return result;
    }
};


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值