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

    class Solution {
    public:
        vector<string> fullJustify(vector<string> &words, int L) 
        {
            vector<string> result;
            int index=0;
            while(index<words.size())
            {
                vector<string> line;
                //find line words
                int len=0;
                while(len==0 || (len>0 && words[index].length()+1+len<=L))
                {
                    if(line.size()!=0)
                    {
                        line.push_back(" ");
                        len++;
                    }
                    len=len+words[index].length();
                    line.push_back(words[index]);
                    
                    index++;
                    if(index==words.size()) break;
                }            
                //only 1 word
                if(line.size()==1)
                {
                        string word=line[0];
                        for(int i=0;i<L-line[0].length();i++) word=word+" ";
                        result.push_back(word);
                }
                //more words
                else
                {
                    if(index!=words.size())
                    {
                        int i=1;
                        while(len<L)
                        {
                            line[i]=line[i]+" ";
                            len++;
                            //find a space
                            i=i+2;
                            if(i>=line.size()) i=1
                        }
                    }
                    else
                    {
                        int i=line.size()-1;
                        while(len<L)
                        {
                            line[i]=line[i]+" ";
                            len++;
                        }
                    }
                    string strline;
                    for(int i=0;i<line.size();i++)
                        strline=strline+line[i];
                    result.push_back(strline);
                }            
            }
            return result;
        }
    };
  • 相关阅读:
    mysql创建用户,并赋予权限:只能查某个数据库中的某张表(只读)
    Fastjson toJSONString用单引号进行转换
    MyBatis传入参数为list、数组、map写法
    进制GB和GiB的区别
    leaflet 根据一个经纬度及距离角度,算出另外一个经纬度
    ubuntu下安装YApi
    Oracle 存储过程测试
    Oracle两种临时表的创建与使用详解
    一月到十二月的英文
    spring framework各个版本下载网址
  • 原文地址:https://www.cnblogs.com/erictanghu/p/3759471.html
Copyright © 2011-2022 走看看