zoukankan      html  css  js  c++  java
  • 68. Text Justification *HARD*

    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 maxWidth) {
            int n = words.size(), l, extra, eachex, i, j, k, t;
            vector<int> len(n, 0);
            for(i=0; i<n; i++)
                len[i] = words[i].length();
            vector<string> ans;
            i=0;
            while(i<n)
            {
                for(l=len[i], j=i+1; j<n && l<=maxWidth; j++)
                    l += len[j]+1;
                string s = words[i];
                if(j==n && l<=maxWidth) //the last line
                {
                    for(k = i+1; k<j; k++)
                    {
                        s += ' ';
                        s += words[k];
                    }
                }
                else
                {
                    j--;
                    l -= len[j]+1;
                    extra = maxWidth-l;
                    eachex = (j>i+1) ? extra / (j-i-1) : extra;
                    for(k=i+1; k<j; k++)
                    {
                        if(extra)
                        {
                            if((j == i) || (extra == eachex*(j-k)))
                            {
                                for(t=0; t<eachex; t++)
                                    s += ' ';
                                extra -= eachex;
                            }
                            else
                            {
                                for(t=0; t<eachex+1; t++)
                                    s += ' ';
                                extra -= eachex+1;
                            }
                            
                        }
                        s += ' ';
                        s += words[k];
                    }
                }
                for(k=s.length(); k<maxWidth; k++)
                    s += ' ';
                ans.push_back(s);
                i = j;
            }
            return ans;
        }
    };

    测试用例:

    ["a","b","c","d","e"] 1 -- ["a","b","c","d","e"]

    ["Listen","to","many,","speak","to","a","few."] 6 -- ["Listen","to    ","many, ","speak ","to   a","few.  "]
    ["Here","is","an","example","of","text","justification."] 16 -- 
    ["Here    is    an","example  of text","justification.  "]

    ["Don't","go","around","saying","the","world","owes","you","a","living;","the","world","owes","you","nothing;","it","was","here","first."]
    30 -- 

    ["Don't  go  around  saying  the","world  owes  you a living; the","world owes you nothing; it was","here first.                   "]
     
  • 相关阅读:
    淘宝npm镜像
    mousedown监听onmousemove判断鼠标指针移动方向的JavaScript
    vue 生命周期流程图 go.js
    微信在pc端打开多窗口.bat
    bootstrap配置
    前端入门到进阶图文教程,超详细的Web前端学习笔记。从零开始学前端,做一名精致优雅的前端工程师。
    低代码开发平台
    开源在线绘图工具,界面美观,功能丰富
    IOS 空字符串报错 解决办法
    vue 在使用数组的时候,数组内部数据发生变化,视图却没有改变
  • 原文地址:https://www.cnblogs.com/argenbarbie/p/5271580.html
Copyright © 2011-2022 走看看