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 环境配置
    批量生成不同尺寸的图片
    如何生成publish windows app 用到的 pfx 文件
    MVC项目用Windsor注入
    UWP textbox 只能输入数字
    power shell upload file to azure storage
    Checkbox can't checked
    安装部署 Goaccess
    阿里云OSS的Bucket容量大小采集
    1. Nagios和 NagiosQL安装及配置
  • 原文地址:https://www.cnblogs.com/superzrx/p/3355970.html
Copyright © 2011-2022 走看看