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 };
  • 相关阅读:
    Linux认知之旅【02 装个软件玩玩】!
    Linux认知之旅【01 与Linux第一次亲密接触】!
    C# Modbus 之 EasyModbus
    C# 串口读取并转换字符串
    java api 接口 postman @RequestBody
    html JavaScript 点击图片放大,点击图片缩小
    C# 请求 form-data格式的 接口 POSTMAN form-data
    java.lang.NoSuchMethodError: org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(Ljava/io/File;Z) 报错处理
    Java net.sf.jxls 生成模板 并导出excel
    前段生成二维码下载,打印 QrCode
  • 原文地址:https://www.cnblogs.com/wxquare/p/6912105.html
Copyright © 2011-2022 走看看