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."]
    L16.

    Return the formatted lines as:

    [
       "This    is    an",
       "example  of text",
       "justification.  "
    ]
    

    Note: Each word is guaranteed not to exceed L in length.

    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.
    贪心,注意细节。
     1 class Solution {
     2 private:
     3     vector<string> ret;
     4 public:
     5     vector<string> fullJustify(vector<string> &words, int L) {
     6         // Start typing your C/C++ solution below
     7         // DO NOT write int main() function
     8         ret.clear();
     9         int i = 0;
    10         while(i < words.size())
    11         {
    12             int size = 0;
    13             int beg = i;
    14             while(i < words.size())
    15             {
    16                 int newSize = size == 0 ? words[i].size() : size + 1 + words[i].size();
    17                 if (newSize <= L)
    18                     size = newSize;
    19                 else
    20                     break;
    21                 i++;
    22             }
    23             
    24             int spaceCount = L - size;
    25             int everyCount;
    26             if (i - 1 - beg != 0 && i != words.size())
    27             {
    28                 everyCount = spaceCount / (i - 1 - beg);
    29                 spaceCount %= (i - 1 - beg);
    30             }
    31             else
    32                 everyCount = 0;
    33             string s;
    34             for(int j = beg; j < i; j++)
    35                 if (j == beg)
    36                     s = words[j];
    37                 else
    38                 {
    39                     s += ' ';
    40                     for(int k = 0; k < everyCount; k++)
    41                         s += ' ';
    42                     if (spaceCount > 0 && i != words.size())
    43                     {
    44                         s += ' ';
    45                         spaceCount--;
    46                     }
    47                     s += words[j];
    48                 }
    49                 
    50             for(int j = 0; j < spaceCount; j++)
    51                 s += ' ';
    52                 
    53             ret.push_back(s);
    54         }
    55         
    56         return ret;
    57     }
    58 };
  • 相关阅读:
    python 利用pyspark读取HDFS中CSV文件的指定列 列名重命名 并保存回HDFS
    python 利用pandas读取本地中CSV文件的指定列 列名重命名 并保存回本地
    CDH版本Hbase二级索引详细配置方案Solr key value index(二)中文分词
    CDH版本Hbase二级索引详细配置方案Solr key value index
    Seccon2017-pwn500-video_player
    Linux ASLR的实现
    0ctf2017-pages-choices
    33c3-pwn500-recurse
    关于C++中的string的小知识点
    Apache Kylin(三)Kylin上手
  • 原文地址:https://www.cnblogs.com/chkkch/p/2776335.html
Copyright © 2011-2022 走看看