zoukankan      html  css  js  c++  java
  • 68. 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.  "
    ]
    思路: 每行能容纳的单词数是,单词长度+单词间的长度不超过maxWidth

    注意数组越界,无限循环,以及除数为零

    class Solution {
    public:
        vector<string> fullJustify(vector<string>& words, int maxWidth) {
            int curLen = 0;
            int count = 0; //number of words in a line
            int spaceNum;
            int residue;
            int start = 0;
            string sLine = "";
            string sSpace = "";
            string sSpaceMore;
            vector<string> ret;
            
            for(int i = 0; i < words.size();){
                if(curLen + words[i].length() <= maxWidth){
                    curLen += words[i].length();
                    curLen++; //space
                    count++;
                    i++;
                }
                else{ //deal with space
                    if(count == 1){ //保证除数!=0,所以这里要分开讨论
                        sLine += words[start++];
                        for(int j = curLen; j <= maxWidth; j++){
                            sLine += ' ';
                        }
                    }
                    else{
                        spaceNum = (maxWidth - curLen+1) / (count-1) + 1; //curLen-1 is the length of words with only one space; count-1 is the interval number
                        residue = (maxWidth - curLen+1) % (count-1);
                        for(int j = 0; j < spaceNum; j++){
                            sSpace += ' ';
                        }
                        sSpaceMore = sSpace + " ";
                        sLine += words[start++];
                        for(int j = 0; j < residue; j++){
                            sLine += sSpaceMore + words[start++];
                        }
                        for(int j = residue; j < count-1; j++ ){
                            sLine += sSpace + words[start++];
                        }
                    }
                    ret.push_back(sLine);
                    sLine = "";
                    sSpace = "";
                    curLen = 0;
                    count = 0;
                    
                }
            }
            
            //last line
            if(start < words.size()){
                sLine += words[start++];
                for(int i = start; i < words.size(); i++){
                    sLine += ' '+words[i];
                }
             
                for(int j = curLen; j <= maxWidth; j++){
                    sLine += ' ';
                }
                ret.push_back(sLine);
            }
            return ret;
        }
    };

    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.  "
    ]
    
  • 相关阅读:
    视频流媒体服务器播放视频或直播为什么要使用编解码?
    安防视频监控直播的画面都是如何采集的?
    为什么说线上教育是未来的趋势?
    音视频流媒体服务器发展到现在有哪些难题?应该怎么解决?
    视频流媒体服务器如何将视频直播转推到其他平台?
    为什么流媒体服务器做安防视频直播或者娱乐直播服务?
    安防音视频流媒体服务器EasyDSS之编解码的基本原理及压缩编码的方法介绍
    区块链保护隐私,我们真的需要吗?
    你可能还是低估了云计算的力量......
    区块链究竟有什么价值体现?
  • 原文地址:https://www.cnblogs.com/qionglouyuyu/p/6024424.html
Copyright © 2011-2022 走看看