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
  • 相关阅读:
    乱七八杂
    转化跟踪设置说明
    Photoshop图象切片保存为网页HTML(DIV+CSS布局)的方法
    HTTP 错误 403.14
    打开asp出现An error occurred on the server when processing the URL
    ADODB.Connection 错误 '800a0e7a'
    修改客户端连接的服务器IP地址(内部使用)
    关于云计算的10个常见问题解答
    云安全的11个挑战及应对策略
    如何鉴别一个区块链项目的真假?
  • 原文地址:https://www.cnblogs.com/superzrx/p/3355970.html
Copyright © 2011-2022 走看看