LeetCode 68 -Text Justification

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.


My Code

class Solution {
public:
    vector<string> fullJustify(vector<string>& words, int maxWidth) {
        vector<string> res, cur;
        string tmp;
        int curWidth = 0, addWidth, remainedWidth, interval;
        int i = 0;
        while (true)
        {
            if (i == words.size())
            {
                if (cur.size() == 0)
                    break;

                string l;
                for (auto& w : cur)
                    l += w;
                l += string(maxWidth - curWidth, ' ');
                res.push_back(l);
                break;
            }

            addWidth = words[i].length();
            if (cur.size() > 0)
            {
                addWidth += 1;
                tmp = string(" ") + words[i];
            }
            else
            {
                tmp = words[i];
                //cout << "tmp:" << tmp;
            }
            curWidth += addWidth;

            if (curWidth < maxWidth)
            {
                cur.push_back(tmp);
                i++;
            }
            else if (curWidth == maxWidth)
            {
                string l;
                for (auto& w : cur)
                    l += w;
                l += tmp;
                res.push_back(l);
                curWidth = 0;
                cur.clear();

                i++;
            }
            else
            {
                curWidth -= addWidth;

                string l = cur[0];
                if (cur.size() == 1)
                    l += string(maxWidth - curWidth, ' ');
                else
                {
                    remainedWidth = maxWidth - curWidth;
                    interval = remainedWidth / (cur.size() - 1);
                    remainedWidth %= (cur.size() - 1);
                    for (int j = 1; j < cur.size(); j++)
                    {
                        if (remainedWidth > 0)
                        {
                            l += string(interval + 1, ' ') + cur[j];
                            remainedWidth--;
                        }
                        else
                            l += string(interval, ' ') + cur[j];
                    }
                }
                res.push_back(l);
                curWidth = 0;
                cur.clear();
            }
        }
        return res;
    }
};
Runtime: 0 ms

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EnjoyCodingAndGame

愿我的知识,成为您的财富!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值