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     }
  • 相关阅读:
    质量属性的六个常见属性应用场景(淘宝篇)
    软件架构师如何工作?
    寒假学习第十五天
    寒假学习第十四天
    ADMEMS方法体系:3个阶段,一个贯穿环节之Refined Architecture阶段阅读感悟
    《企业应用架构模式》阅读笔记一
    《重构:改善既有代码的设计》阅读笔记三
    大数据分析01——数据爬取
    《重构:改善既有代码的设计》阅读笔记二
    kettle--Trans插件之输出
  • 原文地址:https://www.cnblogs.com/waruzhi/p/3457788.html
Copyright © 2011-2022 走看看