• Text Justification


    题目:

    Given an array of words and a length L, format the text such that each line has exactlyL 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."]
    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.

    分析:先计算出每行应该放几个单词,对于只有一个单词的情况比较好处理,对于有好几个单词,得计算每个单词后得放几个空格。

    代码如下:

            vector<string> fullJustify(vector<string> &words, int L) {
            vector<string> result;
            for(int i=0;i<words.size();)
            {
                int tmpl=0,j=i;//templ表示当前字符串长度
                for(;j<words.size();j++)
                {
                    tmpl+=words[j].length();
                    if(tmpl<L)
                    {
                        tmpl+=1;
                    }
                    else
                    {
                        if(tmpl==L)
                            break;
                        else
                        {
                            tmpl-=words[j].length();
                            tmpl-=1;
                            j--;
                            break;
                        }
                    }
                }
                if(j==words.size())
                {
                    string tmps;
                    int t=i;
                    for(;t<j;t++)
                    {
                        tmps+=words[t];
                        if(tmps.length()<L)
                        {
                            tmps+=" ";
                        }
                    }
                    while(tmps.length()<L)
                    {
                        tmps+=" ";
                    }
                    result.push_back(tmps);
                    break;
                }
                else
                {
                    string tmps;
                    if(i==j)
                    {
                        tmps+=words[i];
                        while(tmps.length()<L)
                        {
                            tmps+=" ";
                        }
                        result.push_back(tmps);
                    }
                    else
                    {
                        int t=i;
                        int * array=new int [j-t];
                        int len;
                        int mod = (L-tmpl+j-t)%(j-t);
                        int modresult=(L-tmpl+j-t)/(j-t);
                        for(int k=0;k<j-t;k++)
                        {
                            if(mod>0)
                            {
                                array[k]=modresult+1;
                                mod--;
                            }
                            else
                            {
                                array[k]=modresult;
                            }
                        }
                        for(;t<=j;t++)
                        {
                           tmps+=words[t];
                           if(tmps.length()<L)
                           {
                               while(array[t-i]!=0)
                               {
                                   tmps+=" ";
                                   array[t-i]--;
                               }
                           }
                        }
                        delete []array;
                        result.push_back(tmps);
                    }             
                }
                i=j+1;
            }
            return result;
        }

  • 相关阅读:
    TestNG 入门教程
    Angularjs中文版本开发指南发布
    你是码农 还是优秀程序员?
    java牛人给新人的几点建议
    有一种毒药叫励志书
    牛人眼中如何精通spring?
    驯服你的Windows Server 2003
    windows 7 里面的iis在哪里
    神器 cmder
    亚信UED前端流程自动化构建工具
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3091539.html
Copyright © 2020-2023  润新知