[LeetCode30] Day30 - END

教授说12点前要看到实验结果,但我组长看我不先发文,也想把我杀了,人真难做,我只好先来发文QQ

心得

经过这30天,说长不长,说短不短,感谢队友的激励,让我能每天都在与时间赛跑中发文。
从中学习了一些演算法,动了脑,虽然我的介绍与解释不佳,但还是有人愿意拨空看一下,万分感激!!~
虽然说是最後一天,组长跟我说打打心得就好,但我还是想放个题目。

题目 68. Text Justification

下面是原文,简单就是一行可以这麽多字 (maxWidth),知道word的左右对齐吧,阿要尽量能塞字就塞字在同行,但不能超过maxWidth
给你一个array,储存word,依序组成文本。

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

Note:

A word is defined as a character sequence consisting of non-space characters only.
Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
The input array words contains at least one word.

解释 & Code

这里采用我觉得挺暴力的方式,i是words array的index。在一个while中,我们先用另一个j,是指向i下个单字,并将单字的长度加起来(sum),没超过就继续往後。阿要记得多加1(单字间的空格也算1字元)。
然後就将这些单字组成一个string并储存进ans array(即完成一行),如果已经到最後一行了,一样要填满空白哦~

class Solution {
public:
    vector<string> fullJustify(vector<string>& words, int maxWidth) {
        vector<string> ans;
        int i = 0; //words pointer
        int spaceNum = 0;
        int spaceNumMod = 0;
        bool last = false;
        while (i < words.size()) {
            int sum = words[i].length();
            int j = i + 1;
            while (j < words.size() && sum + words[j].length() + 1 <= maxWidth) {
                sum += (words[j].length() + 1);
                j++;
            }
            j--;
            if (j >= words.size() - 1) {
                last = true;
            }
            int gap = j - i;
            int spaceNum = 0;
            int spaceNumMod = 0;
            if (gap != 0) {
                sum -= gap;
                spaceNum = (maxWidth - sum) / gap - 1;
                spaceNumMod = (maxWidth - sum) % gap;
            }
            string tmp = "";
            string space(spaceNum, ' ');
            while (i < j) {
                tmp += (words[i] + " ");
                if (!last) {
                    tmp += space;
                }
                if (!last && spaceNumMod > 0) {
                    tmp += " ";
                    spaceNumMod--;
                }
                i++;
            }
            tmp += words[j];
            if (tmp.length() != maxWidth) {
                tmp += string(maxWidth-tmp.length(), ' ');
            }
            ans.push_back(tmp);
            i++;
        }
        return ans;
    }
};

<<:  LeetCode 581. Shortest Unsorted Continuous Subarray

>>:  Day 30 |> 完赛心得

[iT铁人赛Day29]练习题(8)

时间过得真快,一转眼就来到做练习题的最後一天,明天要做最後总结 最後一天要来做练习题第八题 第八题要...

数字证书(Digital Certificate)

证书申请和回应 证书签名请求(Certificate Signing Request) 在公钥基础结...

Day 16 - Reverse String

大家好,我是毛毛。ヾ(´∀ ˋ)ノ 废话不多说开始今天的解题Day~ 344. Reverse St...

# Day28--让commit像战国时代一样分分合合

上一篇我们学到怎麽使用Vim,还有修改commit message,这次要做的事情呢,就是要来合并跟...

D1: [漫画]工程师太师了-第1话

工程师太师了: 第1话 杂记: 本来打好主意每天来PO一张图,等我PO完30天,就可以完赛了。 今天...