zoukankan      html  css  js  c++  java
  • 面试题34:文本对齐

    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."]
    L:16.

    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 class Solution {
     2 public:
     3     vector<string> fullJustify(vector<string> &words, int L) {
     4         vector<string> res;
     5         
     6         
     7         int size = words.size();
     8         if (size == 0) return res;
     9         int i = 0,j=0,sum=0;
    10 
    11         while (j <= size) {
    12             if (j == size || sum + j - i + int(words[j].length()) > L) {
    13                 string tmp;
    14                 if (j == size) {
    15                     for (int k = i; k < j; k++) {
    16                         if(k != i)
    17                             tmp += " ";
    18                         tmp += words[k];
    19                     }
    20                     while(tmp.size() < L) tmp += " ";
    21                     res.push_back(tmp);
    22                     return res;
    23                 }
    24                 
    25                 int diff = L - sum;
    26                 if (j - i <= 2) {
    27                     tmp = words[i];
    28                     while (diff > 0) {
    29                         tmp += " ";
    30                         diff--;
    31                     }
    32                     if (j - i == 2) {
    33                         tmp += words[j - 1];
    34                     }
    35                     res.push_back(tmp);
    36                 } else {
    37                     int a = diff / (j - i - 1);
    38                     int b = diff % (j - i - 1);
    39                     for (int k = 0; k < j - i - 1; k++) {
    40                         tmp += words[i + k];
    41                         for (int t = a; t > 0; t--) {
    42                             tmp += " ";
    43                         }
    44                         if (k < b)
    45                             tmp += " ";
    46                     }
    47                     tmp += words[j - 1];
    48                     res.push_back(tmp);
    49                 }
    50                 i = j;
    51                 sum = words[j].length();
    52                 j++;
    53             } else {
    54                 sum += words[j].length();
    55                 j++;
    56             }
    57         }
    58         return res;
    59     }
    60 };
  • 相关阅读:
    Loadrunner 9.5_webservice(SOAP)性能测试
    oracle分层查询中的start with和connect by(树结构查询)
    解析Nginx负载均衡
    Nginx+tomcat配置集群负载均衡
    基于Nginx反向代理及负载均衡
    什么是反向代理,如何区别反向与正向代理
    软件测试策略
    软件测试策略的制定过程
    php 模拟get和post提交方法[解决ajax跨域问题]
    解决ajax跨域问题的多种方法
  • 原文地址:https://www.cnblogs.com/wxquare/p/6912105.html
Copyright © 2011-2022 走看看