zoukankan      html  css  js  c++  java
  • LeetCode-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 exactly L 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) {
            // Note: The Solution object is instantiated only once and is reused by each test case.
            int i=0,j=0,len=0;
            string s;
            vector<string>ret;
            if(words.size()==0){
                s.clear();
                s.resize(L,' ');
                ret.push_back(s);
                return ret;
            }
            if(words.size()==1){
                s.clear();
                s+=words[0];
                while(s.length()!=L)s+=' ';
                ret.push_back(s);
                return ret;
            }
            while(true){
                if(j==words.size()){
                    len=0;
                    s.clear();
                    s+=words[i];
                    len+=words[i].length();
                    for(int k=i+1;k<j;k++){
                        s+=' ';
                        len++;
                        s+=words[k];
                        len+=words[k].length();
                    }
                    for(int k=0;k<L-len;k++)s+=' ';
                    ret.push_back(s);
                    break;
                }
                else if(len+words[j].length()>=L){
                    if(len+words[j].length()==L)j++;
                    len=0;
                    int total=j-i-1;
                    for(int k=i;k<j;k++){
                        len+=words[k].length();
                    }
                    s.clear();
                    s+=words[i];
                    if(total==0){
                        for(int k=0;k<L-len;k++){
                            s+=' ';
                        }
                        ret.push_back(s);
                    }
                    else{
                        int ea=(L-len)/total;
                        int fr=(L-len)%total;
                        for(int k=i+1;k<j;k++){
                            for(int m=0;m<ea;m++){
                                s+=' ';
                            }
                            if(fr!=0){
                                s+=' ';
                                fr--;
                            }
                            s+=words[k];
                        }
                        ret.push_back(s);
    
                    }
                    i=j;
                    len=0;
                    if(j==words.size())break;
                }
                else{
                    len+=words[j].length()+1;
                    j++;
                }
            }
            return ret;
        }
    };
    View Code
  • 相关阅读:
    Hadoop一直处于安全模式(hadoop去掉保护模式)
    日考
    MySQL中文编码
    Mussel使用系列(二):开始写我们的第一个Mussel插件项目
    什么是Mussel
    Mussel使用系列(四):从容器中访问Mussel的插件项目
    Mussel使用系列(三):Mussel插件树的构成及初步使用
    Mussel使用系列(五):插件项目之间的调用
    Mussel使用系列(一):Mussel配置文件演示
    C#委托探索之猫和老鼠
  • 原文地址:https://www.cnblogs.com/superzrx/p/3355970.html
Copyright © 2011-2022 走看看