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;
        }

  • 相关阅读:
    网络文件传输方式
    ETL利器Kettle
    oracle 字符处理
    ORACLE临时表空间
    Count(*)或者Count(1)或者Count([列]) 区别
    Oracle trunc()函数的用法
    DATE 日期格式
    oracle 异常
    物化视图
    域名和端口
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3091539.html
Copyright © 2011-2022 走看看