LeetCode OJ:Text Justification


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

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、考虑到字符串间的空格个数不一样,比如如果有4个单词,需要5个空格,那么分配应该是2,2,1,可以先每个分配一个空格,然后从前往后看余数是否为0,不为0,个数加1,被除数减1;

2、每个单词间隔至少为1个空格,第一个单词前没有空格,最后一个单词后没有空格,可以用str保存已经访问过的单词(中间有一个空格),用一个队列保存已经访问过的单词,用last保存正在访问的单词

class Solution {
public:
    vector<string> fullJustify(vector<string> &words, int L) {
        vector<string> result;
        string str,last;
        queue<string> que;
        for(int i=0;i<words.size();i++){
            if(words[i]=="")continue;
            if(last==""){
                last=words[i];
                continue;
            }
            if(str==""&&str.size()+last.size()+words[i].size()<L){
                str+=last;
                que.push(last);
                last=words[i];
            }
            else if(str!=""&&str.size()+last.size()+words[i].size()<L-1){
                str+=" "+last;
                que.push(last);
                last=words[i];
            }
            else{
                if(str==""){
                    str=last;
                    while(str.size()<L)str+=" ";
                }
                else{
                    int count=L-(str.size()-(que.size()-1)+last.size());//总空格数
                    int size=que.size();
                    int cnt=count/que.size();//单词间最少需要的空格数
                    str="";
                    while(!que.empty()){
                        string t=que.front();que.pop();
                        str+=t;
                        for(int i=1;i<=cnt;i++)str+=" ";
                        if(count%size){
                            str+=" ";
                            count--;
                        }
                    }
                    
                    str+=last;
                }
                
                result.push_back(str);
                str="";
                last=words[i];
                while(!que.empty())que.pop();
            }
        }
        if(str=="")str+=last;
        else str+=" "+last;
        while(str.size()<L)str+=" ";
        result.push_back(str);
        return result;
    }
};


LeetCode OJ:Text Justification

上一篇:POJ 2774 两个字符串的最长公共子序列。


下一篇:罐子与硬币--【英雄会】