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     }
  • 相关阅读:
    【转】折腾好windows 7和windows 2003双系统
    【转】ndis 相关资料
    【转】自己开发程序管理WINDOWS防火墙
    你给信息工程专业本科大一的学生推荐些什么书?
    【转】如何禁止指定IP访问我的计算机
    【转】一个C#写的调用外部进程类
    【转】Crossbow携手微软,全新工具包推进无线传感器应用开发
    【转】RFC整理分类
    【转】国内外物联网技术研究进展
    【链】SmartBits600测试指导书
  • 原文地址:https://www.cnblogs.com/waruzhi/p/3457788.html
Copyright © 2011-2022 走看看