【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."]
L: 16.

Return the formatted lines as:

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

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

解题思路

比价麻烦的字符串处理问题。
需要的结果是,每个单词之间至少有一个空格,最后一行直接left pad,单词为空时也算是一个单词。
每次计算单词加上空格间隔之后,大小与maxWidth之间的关系,考虑如下三种情况。

  • 等于maxWidth。很好,直接每个单词之间加一个空格作为间隔,构造一行。
  • 小于maxWidth。继续向后看一个单词
  • 大于maxWidth。说明前一个单词的时候就已经满足情况了,仔细计算使得空格均匀分布,构造一行。

最后处理一下剩下的最后一行即可。
没什么算法可言,暴力解题。

it teaches you in the real world. Programmers are always been ask to deal with dirty works.

AC代码

class Solution {
public:
    vector<string> fullJustify(vector<string>& words, int maxWidth) {
        vector<string> ans;

        int sum = 0;
        int startIdx = 0;
        for (int i = 0; i < words.size(); ++i) {
            int interval = i - startIdx;
            int totalLen = sum + words[i].size() + interval;
            if (totalLen == maxWidth) {
                string temp = words[startIdx];
                for (int j = startIdx + 1; j <= i; ++j) {
                    temp = temp + " " + words[j];
                }
                ans.push_back(temp);
                sum = 0;
                startIdx = i + 1;
            }
            else if (totalLen < maxWidth) {
                sum += words[i].size();
            }
            else {
                //push back word from startIdx to i - 1
                if (interval == 1) {
                    string temp = words[i - 1];
                    temp.resize(maxWidth, ' ');
                    ans.push_back(temp);
                }
                else {
                    int intervalSize = maxWidth - sum;
                    // 计算间隔的空格大小,使其均匀分布
                    int spaceSize = intervalSize / (interval - 1);
                    int extra = intervalSize % (interval - 1);
                    string temp = words[startIdx];
                    for (int j = startIdx + 1; j < i; ++j) {
                        if (j - startIdx <= extra) {
                            string pad(spaceSize + 1, ' ');
                            temp += (pad + words[j]);
                        }
                        else {
                            string pad(spaceSize, ' ');
                            temp += (pad + words[j]);
                        }
                    }
                    ans.push_back(temp);
                }
                sum = words[i].size();
                startIdx = i;
            }
        }

        if (startIdx < words.size()) {
            //push back startIdx to end - 1 as last line
            string temp = words[startIdx];
            for (int i = startIdx + 1; i < words.size(); ++i) {
                temp += (" " + words[i]);
            }
            temp.resize(maxWidth, ' ');
            ans.push_back(temp);
        }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值