zoukankan      html  css  js  c++  java
  • 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 Lcharacters.

    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.
    首先要做的就是确定每一行能放下的单词数,这个不难,就是比较n个单词的长度和加上n - 1个空格的长度跟给定的长度L来比较即可,找到了一行能放下的单词个数,然后计算出这一行存在的空格的个数,是用给定的长度L减去这一行所有单词的长度和。得到了空格的个数之后,就要在每个单词后面插入这些空格,这里有两种情况,比如某一行有两个单词"to" 和 "a",给定长度L为6,如果这行不是最后一行,那么应该输出"to   a",如果是最后一行,则应该输出 "to a  ",所以这里需要分情况讨论,最后一行的处理方法和其他行之间略有不同。最后一个难点就是,如果一行有三个单词,这时候中间有两个空,如果空格数不是2的倍数,那么左边的空间里要比右边的空间里多加入一个空格,那么我们只需要用总的空格数除以空间个数,能除尽最好,说明能平均分配,除不尽的话就多加个空格放在左边的空间里,以此类推,
    class Solution {
    public:
        vector<string> fullJustify(vector<string> &words, int L){
            vector<string> result;
            // input validation
            if(words.empty()) return result;
             
            // param @ i 遍历单词的下标临时变量
            // param @ k 校正后每行单词个数
            // param @ l 每行字符(除空格)个数
            // 每次遍历构造一行 将单词检索进新数组
            for(int i = 0, k, l; i < words.size(); i += k){
                // 查询单词不能越界 并且每行增添的单词需要满足要满足中间能插入空格
                //
                for(k = 0, l = 0; k+i < words.size() && l + words[i+k].size()<= L-k;
                   l += words[i+k].size(), ++k);
                 
                // 构造新行
                string temp = words[i]; // 每行首单词
                for(int j = 0; j < k-1; ++j){
                    int blanks = (L-l)/(k-1) + (j < (L-l)%(k-1));
                    // 已经是最后一行单词,要求左对齐,每个单词之间一个空格,剩下空格放在最后
                    if(i+k >= words.size()){
                        temp += " ";
                    }
                    else temp += string(blanks, ' ');
                    temp += words[i+j+1];
                }
                // 补全空格
                temp += string(L-temp.size(), ' ');
                result.push_back(temp);               
            }
            return result;
        }
    };
  • 相关阅读:
    tr加不上边框
    placeholder 用法
    <input/>文本输入框效果:
    colspan="2"、列、rowspan="3"、行、用法!
    CSS--实现小三角形
    “div+css”下拉菜单
    HDU4624 Endless Spin(概率&&dp)
    chanme的博客搬家了!
    HDU5487 Difference of Languages(BFS)
    HDU5469 Antonidas(树分治&&哈希)
  • 原文地址:https://www.cnblogs.com/zl1991/p/9630818.html
Copyright © 2011-2022 走看看