leetcode #68 in cpp

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."]

Return the formatted lines as:

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

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


Honestly I do not like this kind of question. Collecting words in into a row is easy. We could simply check if we can put a word in this row. If we could put this word, then we collect this word and reduce the length of available space by the length of this word + 1(length of one space). It is quite frustrating to pad spaces into the row.

There are 2 cases when we pad space:

1. The row is the last row: we do not pad extra space between words. Just append them with 1 space in between. When necessary we append extra spaces to this row to reach maximum length.

2. The row is not the last row. Then we have to calculate how long the space is for each word when we pad the space. It is simply the currently allowable space length / (current number of word +1)to add into the row. Say we have "this", "isn't", "a", "dog", and length is 16. 

we know that 'this' must be at the front and 'dog' must be at the end. Before we insert 'a in front of 'dog', there are 16 - len(this)-len(dog) = 16 - 7 = 9 allowable space length. We have 2 word to add, and thus the current average length of space we should append is 9/3 = 3 when we add 'a'. Thus we append 3 spaces to 'a', and insert "a _ _ _" in front of 'dog'. When we come to 'isn't', the number to add is 1 and current allowable space length is 5 and thus we append 6/2 = 3 spaces. 


class Solution {
    vector<string> fullJustify(vector<string>& words, int maxWidth) {
        vector<string> res;
        if(maxWidth == 0) return words;
        return collect(words, 0, maxWidth,res);
    vector<string> collect(vector<string>&words, int start_ind, int maxWidth, vector<string> &res){
        int cur_length=0;//length of current words in the row
        int end_ind = start_ind; //end_ind points to the ending word of this row
        int i = 0;
        //collect words for this row
        for(int i = start_ind; i < words.size() &&cur_length + words[i].length() <= maxWidth;i++){//check if we can put this word
            if(cur_length + words[i].length() + 1 > maxWidth){//if we can put this word but cannot put an extra space behind it, then this is the end of the setence
                end_ind = i;
            }else{//if we can put one extra space behind it, then this is potentially not the end of a sentence
                end_ind = i;
                cur_length += words[end_ind].length() + 1;//add the word's length and one extra space
        //after collecting the row, add stirngs in the row and pad spaces into the row 
        //we are going to add string beween start_ind and end_ind
        if(end_ind==words.size() - 1){//if it is the last row, we need to follow the 'left' order
            for(int j = start_ind+1; j <= end_ind; j++){//append next words with one space in between
                words[start_ind]+=" "+words[j];
            if(words[start_ind].length() < maxWidth)//fill the rest with space to reach maxWidth
                words[start_ind]+=string(maxWidth -words[start_ind].length(), ' ' );
            res.push_back(words[start_ind]);//push the last row into res and return res
            return res;
        }else{//if this is not the last row
            if(end_ind == start_ind ){//if this string contains only one word, put this word at the front and pad space
                    words[start_ind] +=string(maxWidth - words[start_ind].length(),' ');//pad extra space at the end
            }else{//this row contains more than 1 word
                int space_len = maxWidth;
                for(int i = end_ind; i >= start_ind; i --)
                    space_len -= words[i].length();//length of spaces in this row 
                int temp_end = end_ind-1;
                int avglen;
                //first we calculate how many spaces we should append to current word. Then we add this word to the last word of this row. 
                    avglen = space_len / (temp_end - start_ind+1);//length of space we should append to current word
                    words[end_ind] = words[temp_end] + string(avglen,' ') + words[end_ind];//add this word to the front of the last word in this word
                    temp_end--;//go to next word
                    space_len -= avglen;
                words[start_ind]+= string(space_len,' ')+words[end_ind];//add the first word
        return collect(words, end_ind + 1,maxWidth, res );//go to the next section

文章标签: leetcode cpp
个人分类: interview
上一篇leetcode #67 in cpp
下一篇leetcode #69 in cpp
想对作者说点什么? 我来说一句

LeetCode cpp最新中文题解.pdf

2018年01月10日 866KB 下载