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
  • 相关阅读:
    POJ 1251Jungle Roads
    ES6---Class基本语法
    浅谈JS的toString
    为什么用Object.prototype.toString.call(obj)检测对象类型?
    JS输出内容为[object Object]与toString
    Sublime text JsFormat插件的安装
    Sublime Text 无法使用Package Control或插件安装失败的解决方法
    Emmet-前端开发神器
    sublime text 3中文版配置--插件ChineseLocalizations
    vs code相关用法
  • 原文地址:https://www.cnblogs.com/superzrx/p/3355970.html
Copyright © 2011-2022 走看看