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
  • 相关阅读:
    Kafka~Linux环境下的部署
    Zookeeper~Linux环境下的部署
    pwd显示链接文件的真实路径
    3种不同方式的焦点图轮播
    软件集成策略——如何有效率地提升质量
    寻找直方图中面积最大的矩形 --- 庞果网
    再谈线程
    SQL 常用基础语句
    初识Maven
    公司存在的问题
  • 原文地址:https://www.cnblogs.com/superzrx/p/3355970.html
Copyright © 2011-2022 走看看