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;
    }
    };
     
  • 相关阅读:
    4--Python入门--Python数据集合类型--集合
    3--Python入门--Python数据集合类型--元组
    2--Python入门--Python数据集合类型--列表
    1--Python 入门--Python基础数据类型
    1--Testng功能简介
    2--JAVA+Maven+TestNG搭建接口测试框架搭建
    2--linux命令--查看磁盘空间
    登录功能测试总结
    在Linux环境下搭建Tomcat+mysql+jdk环境
    如何在Linux系统下挂载光盘
  • 原文地址:https://www.cnblogs.com/Vae1990Silence/p/4281490.html
Copyright © 2011-2022 走看看