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 Lcharacters.

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.

click to show corner cases.

Corner Cases:

 

  • A line other than the last line might contain only one word. What should you do in this case?
    In this case, that line should be left-justified.
分析:这道题是细节题,而且是细节比较繁琐的题,实现的时候要深入思考题目要求的意思。
1)首先 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 Lcharacters. 那么我们每一行我们在保证单词总字符数加空格数小于等于L的条件下,pad最多的单词。
2)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. 这个要求的理解很关键,意思是尽量保证空格在单词间是均匀分布的,如果不能做到均匀分布,左侧的空格比右侧的空格多。我们用具体的加空格的例子来说明这个要求。假设L = 14, words = ["abc", "de", "fg", 'hi', 'jklmnopqrst']。在满足第一个要求的情况下,我们第一行有以下几个单词"abc","de","fg","hi"。然后我们需要填充14-9=5个空格,5/3 = 1, 5%3 = 2,空格并不能平均分配到各单词之间,这种情况下按第二个要求,我们需要在第一个和第二个单词之间插入5/3+1个空格,在第二个和第三个单词之间插入5/3+1个空格,第三个和第四个单词之间插入5/3个空格,因为5除以3的余下个空格已经被分配完。
3)For the last line of text, it should be left justified and no extra space is inserted between words. 此要求不是很清楚,特别是第一行又是最后一行的情况。如果第一行是最后一行,我们还是要做justification。
并且,一行只有一个单词的情况需要单词讨论。
class Solution {
public:
    vector<string> fullJustify(vector<string> &words, int L) {
        vector<string> res;
        if(words.size() == 0) return res;
        int line = 0;
        
        for(vector<string>::iterator start = words.begin(); start != words.end(); ){
            line++;
            vector<string>::iterator end = next(start);
            int cur_len = start->length();
            int cur_num = 1;
            while(end != words.end() && cur_len + end->length() + cur_num <= L){
                cur_len += end->length();
                cur_num++;
                end++;
            }
            //only one line or not to the end
            if(line == 1 || end != words.end()){
                int quotient = (cur_num == 1)?0:(L-cur_len)/(cur_num-1);
                int residue = (cur_num == 1)?(L-cur_len):(L-cur_len)%(cur_num-1);
                string tmp;
                for(vector<string>::iterator p = start; p != end; p++){
                    tmp.append(*p);
                    if(next(p) != end){
                        int space_n = (residue > 0)?1:0;
                        tmp.append(string(quotient+space_n, ' '));
                        residue--;
                    }else if(cur_num == 1){
                        tmp.append(string(residue, ' '));
                    }
                }
                res.push_back(tmp);
            }else{//not the first line and to the end
                string tmp;
                for(vector<string>::iterator p = start; p != end; p++){
                    tmp.append(*p);
                    if(next(p) != end){
                        tmp.push_back(' ');
                    }
                }
                if(L-cur_len-cur_num+1 > 0)tmp.append(string(L-cur_len-cur_num+1, ' '));
                res.push_back(tmp);
            }
            start = end;
        }
        return res;
    }
};

 

 

posted on 2014-10-26 17:00  Ryan-Xing  阅读(136)  评论(0编辑  收藏  举报