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;
    }
    };
     
  • 相关阅读:
    下载超过 28762W 次的 Java面试题库(附答案)
    后端程序员必备:SQL高性能优化方案!50条优化,建议马上收藏!
    [ABP教程]第七章 作者:数据库集成
    [ABP教程]第六章 作者:领域层
    [ABP教程]第五章 授权
    [ABP教程]第四章 集成测试
    [ABP教程]第三章 创建、更新和删除图书
    [ABP教程]第二章 图书列表页面
    [ABP教程]第一章 创建服务端
    [Skill] git下载助手
  • 原文地址:https://www.cnblogs.com/Vae1990Silence/p/4281490.html
Copyright © 2011-2022 走看看