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.
    

      分析: 主要注意两个地方。一, 正常的句子单词之间的空格数目是一;二,最后一行句子按正常处理

    class Solution {
    public:
        vector<string> fullJustify(vector<string> &words, int L) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            vector<string> res;
            if(words.size() < 1) return res;
            
            int start , end, len , evenSpace, bonus;
            
            end = 0;
            while(true){
            
                len = 0;
                for(start = end; end < words.size(); ++end){
                    if(len +(end - start) + words[end].length() > L) break;// normal sentence has one space between words
                    len += words[end].length();    
                }
                end--;
                
                if(end == start){ // only one word
                    string line = words[end];
                    line.append(L - len, ' ');
                    res.push_back(line);
                }else{ // multiply words
                    
                    evenSpace = (L - len)/(end - start);
                    bonus = L -len - evenSpace*(end - start);
                    bool isLastLine = (end == words.size() -1);
                    if(isLastLine){
                        evenSpace = 1;
                        bonus = 0;
                    }
                    
                    string line = words[start];
                    for(int i = start + 1; i <= end; ++i)
                    {
                        int space = evenSpace;
                        if(bonus > 0)
                        {
                            --bonus;
                            ++space;
                        }
                        line.append(space,' ');
                        line.append(words[i]);
                    }
                    
                    if(isLastLine){
                        line.append(L-len - (end - start), ' ');
                    }
                    
                    res.push_back(line);
                }
                
                end++;
                if(end == words.size()) break;
            }
            
            return res;        
        }
    };
  • 相关阅读:
    [转]单倍长密钥加密和双倍长密钥加密,银联直联终端62域难点详解
    在ASP.NET MVC中支持 HttpHandler
    点滴积累【SQL Server】---SQL语句操作约束
    点滴积累【other】---Windows 7 IIS (HTTP Error 500.21
    点滴积累【C#】---验证码,ajax提交
    点滴积累【C#】---Highcharts图形统计
    点滴积累【C#】---C#实现下载word
    php无法上传大文件完美解决方案
    PHP上传遇到的问题-php 上传大文件主要涉及配置upload_max_filesize和post_max_size两个选项
    Call to undefined function pg_
  • 原文地址:https://www.cnblogs.com/graph/p/3290497.html
Copyright © 2011-2022 走看看