zoukankan      html  css  js  c++  java
  • leetcode[68]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."]
    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.

    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) 
    {
        if (words.empty()||L<1)
        {
            return words;
        }
        vector<string> res;
        int tempLen;
        string tempStr;
        for (int i=0;i<words.size();)
        {
            tempStr="";
            tempLen=0;
            int j=0;
            int flagj=0;
            while(i<words.size()&&tempLen+words[i].size()<=L)///////while(i<words.size()&&tempLen+words[i].size()<L)
            {
    ///////////////////////////////////////////////////////////////
                 if (tempLen+words[i].size()==L)
                 {
                     if(tempLen) break;
                 }
    ///////////////////////////////////////////////////////////////
                if (tempLen==0)
                {                
                    flagj=i;
                    tempLen+=words[i].size();
                } 
                else
                {
                    j++;
                    tempLen+=(1+words[i].size());
                }
                i++;
            }
            /**
            if (i<words.size()&&tempLen+words[i].size()==L&&tempLen==0)
            {
                flagj=i;
                tempLen+=words[i].size();            
                i++;
            }
            */
            int isSpace=L-tempLen;
            if (isSpace==0)
            {
                for (int ii=flagj;ii<=flagj+j;ii++)
                {
                    if (ii==flagj+j)
                    {
                        tempStr+=words[ii];
                        res.push_back(tempStr);
                    } 
                    else
                    {
                        tempStr+=(words[ii]+' ');
                    }
                }
            }
            else
            {
                if (i==words.size())
                {
                    for (int ii=flagj;ii<flagj+j;ii++)
                    {
                        words[ii]+=' ';
                        tempStr+=words[ii];
                    }
                    tempStr+=words[i-1];
                    tempStr.insert(tempStr.end(), isSpace, ' ');
                    res.push_back(tempStr);
                }
                else
                {
                    if (j==0)
                    {
                        words[i-1].insert(words[i-1].end(),isSpace,' ');
                        tempStr+=words[i-1];
                        res.push_back(tempStr);
                    }
                    else
                    {
                        int len_=isSpace/j;
                        int len_1=isSpace%j;
                        int ii;
                        for (ii=flagj;ii<flagj+len_1;ii++)
                        {
                             words[ii]+=' ';
                        }
                        for (ii=flagj;ii<flagj+j;ii++)
                        {
                            string str1="";
                            str1.insert(str1.end(),len_+1,' ');
                            words[ii]+=str1;
                            tempStr+=words[ii];
                        }
                        tempStr+=words[ii];
                        res.push_back(tempStr);
                    }
                }
            }
        }
        return res;
    }
    };
     
  • 相关阅读:
    使用python在SAE上搭建一个微信应用,使用有道翻译的api进行在线翻译
    使用python一步一步搭建微信公众平台(一)
    Socket 多线程FTP软件开发
    (转)SQL NEWID()随机函数
    (转)ip地址,手机ip查询
    (转)webservice 测试窗体只能用于来自本地计算机的请求
    (转)WITH (NOLOCK)
    (转)jquery.url.js 插件的使用
    (转)SQL中的ISNULL函数介绍
    (转) C# Activator.CreateInstance()方法使用
  • 原文地址:https://www.cnblogs.com/Vae1990Silence/p/4281490.html
Copyright © 2011-2022 走看看