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;
    }
    };
     
  • 相关阅读:
    Git安装配置
    Openstack 错误日志查看方法
    keystone v3.0与2.0的区别
    Python远程调试Openstack
    openstack遇到的错误
    特别翔实的adaboost分类算法讲解 转的
    h5 html5 模拟时钟 页面
    js 面向对象 jquery 全局变量 封装
    HTML5 h5 微信 浮层 提示 点击右上角,从浏览器打开 pop.png
    jquery中ajax使用error调试错误的方法,实例分析了Ajax的使用方法与error函数调试错误的技巧
  • 原文地址:https://www.cnblogs.com/Vae1990Silence/p/4281490.html
Copyright © 2011-2022 走看看