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     }
  • 相关阅读:
    zabbix邮件报警功能的验证
    linux下拷贝命令中的文件过滤操作记录
    Docker容器数据卷-Volume小结
    Elasticsearch集群监控指标学习
    MySQL 更换MyISAM存储引擎为Innodb的操作记录
    MySQL 占用过高CPU时的优化手段
    MySQL 连接数设置操作(Too many connections)及设置md5值的加密密码
    Android Studio aidl文件路径自定义问题
    Android资源混淆 + 混淆忽略 .so库
    Android Studio 换主题(Material Theme..)
  • 原文地址:https://www.cnblogs.com/waruzhi/p/3457788.html
Copyright © 2011-2022 走看看