zoukankan      html  css  js  c++  java
  • 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;
        }

  • 相关阅读:
    音频player
    重要 sdl音最简
    sdl win 播放video hello--
    linux里helloworld
    0129 总结Cygwin要用这个,不要用mingw,根本下不下来
    转音视频重要
    Gradle项目无法下载jar包 修改 插件下载地址plugins.gradle.org

    开源gradle**all.zip下载慢问题
    [Effective Java] 第1条 考虑用静态工厂方法代替构造函数
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3091539.html
Copyright © 2011-2022 走看看