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

    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.

    思路:

    需要注意,如果某一行只有一个字母,那么用空格把剩余的部分填满。

    代码:

     1     vector<string> fullJustify(vector<string> &words, int L) {
     2         int num = words.size();
     3         vector<string> result;
     4         int i,j,k;
     5         int tmp = 0, total = 0;
     6         int last = 0;
     7         for(i = 0; i < num; i++){
     8             if(tmp == 0)
     9                 tmp += words[i].length();
    10             else
    11                 tmp += (words[i].length()+1);
    12             total += words[i].length();
    13             if(tmp > L){
    14                 tmp -= (words[i].length()+1);
    15                 total -= words[i].length();
    16                 int spaceNum = L - total;
    17                 if(i == last+1){
    18                     string line = words[last];
    19                     int curlen = words[last].length();
    20                     while(curlen < L){
    21                         curlen++;
    22                         line += " ";
    23                     }
    24                     result.push_back(line);
    25                     last = i;
    26                     tmp = 0;
    27                     total = 0;
    28                     i--;
    29                     continue;
    30                 }
    31                 int quotient = spaceNum/(i-last-1);
    32                 int remain = spaceNum%(i-last-1);
    33                 string line = words[last];
    34                 for(j = last + 1; j < i; j++){
    35                     for(k = 0; k < quotient; k++)
    36                         line += " ";
    37                     if(j-last <= remain)
    38                         line += " ";
    39                     line += words[j];
    40                 }
    41                 result.push_back(line);
    42                 last = i;
    43                 i--;
    44                 tmp = 0;
    45                 total = 0;
    46             }
    47         }
    48         if(tmp <= L && tmp >= 0){
    49             string line = "";
    50             int curlen = 0;
    51             for(i = last; i < num; i++){
    52                 line += words[i];
    53                 curlen += words[i].length();
    54                 if(curlen < L){
    55                     curlen++;
    56                     line += " "; 
    57                 }
    58             }
    59             while(curlen < L){
    60                 curlen++;
    61                 line += " ";
    62             }
    63             result.push_back(line);
    64         }
    65         return result;
    66     }
  • 相关阅读:
    [精品推荐]Android Studio插件整理
    Android星星评分控件RatingBar的使用
    Android 数据库升级解决方案
    Android版本升级同时Sqlite数据库的升级及之前数据的保留
    Android 热补丁动态修复框架小结
    实现判断条件中有in的判断
    066 基于checkpoint的HA机制实现
    065 updateStateByKey的函数API
    064 SparkStream与kafka的集成,主要是编程
    063 SparkStream数据接收方式
  • 原文地址:https://www.cnblogs.com/waruzhi/p/3457788.html
Copyright © 2011-2022 走看看