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;
        }
    };
  • 相关阅读:
    操作系统指纹
    扫描工具
    ms08_067利用过程
    SQL注入攻击
    SMB/CIFS协议解析
    蓝桥杯 历届试题 大臣的旅费
    九度oj 题目1009:二叉搜索树
    蓝桥杯 算法提高 6-17 复数四则运算
    poj 2182 Lost Cows
    poj 2501 Average Speed
  • 原文地址:https://www.cnblogs.com/erictanghu/p/3759471.html
Copyright © 2011-2022 走看看