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 };
  • 相关阅读:
    [LeetCode] 216. 组合总和 III
    [LeetCode] 215. 数组中的第K个最大元素
    [LeetCode] 215. 数组中的第K个最大元素
    [LeetCode] 215. 数组中的第K个最大元素
    [LeetCode] 213. 打家劫舍 II
    [LeetCode] 212. 单词搜索 II
    [LeetCode] 211. 添加与搜索单词
    转:十大编程算法助程序员走上高手之路
    推荐用于格式化以及高亮显示SQL文的PHP类-SqlFormatter
    转:实用 .htaccess 用法大全
  • 原文地址:https://www.cnblogs.com/wxquare/p/6912105.html
Copyright © 2011-2022 走看看