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;
    }
    };
     
  • 相关阅读:
    B. Connecting Universities DFS,无向树
    HDU 5808 Price List Strike Back bitset优化的背包。。水过去了
    uva 6910
    HDU 5778 abs 数学
    Invitation Cards POJ 1511 SPFA || dij + heap
    HDU 2243 考研路茫茫——单词情结 求长度小于等于L的通路总数的方法
    cmd链接mysql
    如何设置远程访问mysql
    ERROR 1045 (28000): Access denied for user'root'@'localhost'(using password:YES)51Testing软件测试网-H*?U)}$h }P6H4H
    String ,StringBuffer,StringBuilder的区别(转)
  • 原文地址:https://www.cnblogs.com/Vae1990Silence/p/4281490.html
Copyright © 2011-2022 走看看