码迷,mamicode.com
首页 > 其他好文 > 详细

【leetcode】Text Justification(hard) ☆

时间:2015-04-14 19:35:33      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

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.

 

思路:

写了一天,用100多行才搞定。大神直接10+行秒杀我..... 先看大神代码。 

关键点:

①循环内一次性循环出该行的单词

②string 可以初始化指定的空格数

③对最后一行 和 其他行的区分 通过  i + k >= words.size() 只有空格数不同

vector<string> fullJustify(vector<string> &words, int L) {
    vector<string> res;
    for(int i = 0, k, l; i < words.size(); i += k) {
        for(k = l = 0; i + k < words.size() and l + words[i+k].size() <= L - k; k++) {
            l += words[i+k].size();
        }
        string tmp = words[i];
        for(int j = 0; j < k - 1; j++) {
            if(i + k >= words.size()) tmp += " "; //当前是最后一行 空格1个
            else tmp += string((L - l) / (k - 1) + (j < (L - l) % (k - 1)),  );  //不是最后一行 均匀空格 如果有多余的匀给前面
            tmp += words[i+j+1];
        }
        tmp += string(L - tmp.size(),  ); //一行最后面如果还有地方 一定是最后一行的空格
        res.push_back(tmp);
    }
    return res;
}

 

我的代码,其实思路都差不多的。这道题没什么技巧。但是写得非常繁琐。

vector<string> fullJustify(vector<string> &words, int L) {
        vector<string> ans;
        int WordsNum = 0;
        int WordLength = 0;
        int totalLength = 0;
    
        if(L == 0)
        {
            ans.push_back("");
            return ans;
        }

        for(int i = 0; i < words.size(); i++)
        {
            totalLength += words[i].size();
            if(WordLength + WordsNum + words[i].size() == L) //恰好放下新的词 这一行确定
            {
                string LineAns;
                for(int j = i - WordsNum; j < i; j++)
                {
                    LineAns += words[j] + " ";
                }
                LineAns += words[i];
                ans.push_back(LineAns);
                
                //初始化下一行的数据
                WordsNum = 0;
                WordLength = 0;
            }
            else if(WordLength + WordsNum + words[i].size() > L) //放不下新的词了
            {
                string LineAns;
                string sGap;
                
                if(WordsNum == 1) //只有1个词 最右边补空格
                {
                    LineAns += words[i - 1];
                    int n = L - words[i - 1].size();
                    while(n > 0)
                    {
                        LineAns += " ";
                        n--;
                    }
                    ans.push_back(LineAns);
                }
                else
                {
                    int Gap = L - WordLength;
                    int Res = Gap % (WordsNum - 1);
                    int BaseGap = Gap / (WordsNum - 1);            

                    while(BaseGap > 0)
                    {
                        sGap += " ";
                        BaseGap--;
                    }
                    for(int j = i - WordsNum; j < i - 1; j++)
                    {
                        LineAns += words[j] + sGap;
                        if(Res > 0)
                        {
                            LineAns += " ";
                            Res--;
                        }
                    }
                    LineAns += words[i - 1];
                    ans.push_back(LineAns);
                }
                //初始化下一行的数据
                WordsNum = 1;
                WordLength = words[i].size();
            }
            else
            {
                WordsNum++;
                WordLength += words[i].size();
            }
        }

        //处理最后一行
        string LineAns;
        string sGap;
        
        if(WordsNum == 0)
        {
            if(totalLength == 0) //没有内容 输出一行空格
            {
                int n = L;
                while(n > 0)
                {
                    LineAns += " ";
                    n--;
                }
                ans.push_back(LineAns);
            }
        }
        else
        {
            for(int j = words.size() - WordsNum; j < words.size() - 1; j++)
            {
                LineAns += words[j] + " ";
            }
            LineAns += words.back();
            while(LineAns.size() < L)
            {
                LineAns += " ";
            }
            ans.push_back(LineAns);            
        }

        return ans;
    }

 

【leetcode】Text Justification(hard) ☆

标签:

原文地址:http://www.cnblogs.com/dplearning/p/4425782.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!