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     }
  • 相关阅读:
    Win10或Win8下ObjectARX2015 Wizard向导创建项目失败解决方法
    对用友二次开发的初次认识
    我对asp.net管道模型的理解
    wblockCloneObjects 写块克隆的使用
    acedCommand 之使用镜像命令
    MFC之动态调用自己写的类库中的类的成员方法
    objectarx 天正的墙转梁线
    objectarx 之让用户自定义插件命令
    MFC之多字节和宽字节的总结
    MFC之使用blat发送邮件
  • 原文地址:https://www.cnblogs.com/waruzhi/p/3457788.html
Copyright © 2011-2022 走看看