[LeetCode] 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 exactlyL 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.

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.

 

 

Hide Tags
 String
 
思路:在当前节点判断当前能否加入前面的line中,如果能加入(长度不超过L),就update len,如果不能,则计算当前start 到end 的字符串。处理字符串时注意两点,1. 当start==end时,2,当end==n-2,即当前line为最后一行时,另外注意一个技巧,因为我们在当前节点不能处理是,处理start到end,且end==i-1,所以我们要事先压入一个等于L长的字符串才能处理到原有的最后一个字符串。另外,处理完成后,要将i--,重新处理当前i,使其成为first。 整体上感觉不是很难,但要一次写对很难。
class Solution {
    public:
        vector<string> fullJustify(vector<string> &words, int L)
        {

            words.push_back(string(L, 'a'));
            size_t n = words.size();
            int len = 0;
            bool isFirst = true;
            vector<string> res;
            int start = 0, end = 0;
            int remainder = 0;
            string str;


            for(int i = 0; i < n; i++)
            {
                if(isFirst)
                {
                    isFirst = false;
                    len = words[i].size();
                    start = i;
                    //cout << "start\t" << start << endl;
                }
                else
                {
                    if(words[i].size() + 1 + len <= L)
                        len += words[i].size() + 1;
                    else
                    {
                        end = i - 1;
                        remainder = L - len;
                        if(start == end) // only one world
                        {
                            str = words[start];
                            for(int j = 0; j < remainder; j++)
                                str += ' ';

                            res.push_back(str);
                            isFirst = true;
                            str.clear();
                            len = 0;

                            i--; //handle from the start begin;
                            continue;
                        }

                        //cout << "start\t" << start << endl;
                        //cout << "end\t" << end << endl;
                        //cout << "n\t" << n<< endl;

                        if(end == n-2) // the last line
                        {
                            for(int j = start; j <= end; j++)
                            {
                                if(j == start)
                                    str += words[j];
                                else
                                    str += ' ' + words[j];
                            }
                            for(int j = 0; j < remainder; j++)
                                str += ' ';

                        }
                        else
                        {
                            int average = remainder/(end - start );
                            remainder = remainder%(end - start );
                            for(int j = start; j <= end; j++)
                            {
                                if(j == start)
                                    str += words[j];
                                else
                                {
                                    int tmp = average;
                                    while(tmp > 0)
                                    {
                                        str += ' ';
                                        tmp--;
                                    }
                                    if(remainder > 0)
                                    {
                                        str += ' ';
                                        remainder --;
                                    }
                                    str += ' ';
                                    str += words[j];
                                }
                            }
                        }

                        //cout << "str:" << str<< endl;
                        res.push_back(str);
                        isFirst = true;
                        str.clear();
                        len = 0;

                        i--; //handle from the start begin;
                    }
                }
                //cout << "len\t" << len<< endl;

            }

            return res;

        }
};

 

 
posted @ 2015-03-10 10:23  穆穆兔兔  阅读(151)  评论(0编辑  收藏  举报