[leetcode] 68.Text Justification

来源:互联网 发布:中国光伏产业联盟数据 编辑:程序博客网 时间:2024/06/06 02:01

题目:
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.

题意:
给一个字符串数组,以及一个数字L,重新格式化这些字符串,使得每行有L个字符,并且每一行最左边的单词左对齐,每一行最右边的单词右对齐。当然,如果这一行只有一个单词,那么只需要满足左对齐。单词之间的空格数需要尽量均衡,如果不能均分,那么左边的那些空格比右边的那些空格多。最后一行只要左对齐,不需要补空格。另外所有的单词长度都不会超过L。

思路:
这道题比如一行能放L个字符,那么每两个字符串之间至少需要一个空格,那么如果有n个单词,那么最少需要长度是:所有单词的长度之和加上空格的个数。所以对于每一行,需要先去扫描能够存放几个字符串,然后分配空格,比如这一行需要存放n个字符串,还有k个空格(k >= n-1),那么每两个单词间都至少有k/(n-1)个空格,还剩k - k/(n-1)个空格分在前k-k/(n-1)的空格处。有个特例是在非最后一行,只能存放一个单词,那么需要在后面补充空格。

以上。
代码如下:

class Solution {public:    vector<string> fullJustify(vector<string>& words, int maxWidth) {        vector<string> result;        if (words.size() == 1){            string s = words[0];            if (maxWidth - s.size() > 0){                string ss(maxWidth - s.size(), ' ');                s += ss;            }            result.push_back(s);            return result;        }        if (words.size() == 0 || maxWidth <= 0)return result;        int i = 0;        while (i < words.size()){            int j = i;            int length = words[i].size();            int blanks = 0;            while (length <= maxWidth && ++j < words.size()){                ++blanks;                length += words[j].size();                ++length;//This is the blank            }            length = (j >= words.size()) ? length : (length - 1 - words[j].size());            blanks -= (j >= words.size()) ? 0 : 1;            if (j == words.size()){                string res = "";                for (int k = i; k < j - 1; ++k){                    res += words[k];                    res += " ";                }                res += words[j - 1];                string t(maxWidth - length, ' ');                res += t;                result.push_back(res);            }            else if (j == i + 1){//there is only one word in this line                string res = words[i];                if (maxWidth - words[i].size() != 0){                    string t(maxWidth - words[i].size(), ' ');                    res += t;                }                result.push_back(res);            }            else{                int total_blank = maxWidth - length + blanks;                int each_blank = total_blank / blanks;                total_blank %= blanks;                int index = 0;                string res = "";                for (int k = i; k < j - 1; ++k){                    res += words[k];                    string t(each_blank, ' ');                    t += (k - i < total_blank ? " " : "");                    res += t;                }                res += words[j - 1];                result.push_back(res);            }            i = j;        }        return result;    }};
0 0