【LeetCode】68. Text Justification

来源:互联网 发布:广州淘宝摄影培训 编辑:程序博客网 时间:2024/05/23 14:35

题目描述

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
原创粉丝点击