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 };
  • 相关阅读:
    oracle中视图v$sql的用途
    maven 安装jar包命令
    Maven常见异常及解决方法
    对oracle实例的内存(SGA和PGA)进行调整,优化数据库性
    oracle中lock和latch的用途
    oracle中awr性能报告阅读笔记
    oracle 查看并行sql语句的并行数量和如何开并行
    DBMS_STATS.GATHER_TABLE_STATS详解
    关于加快INSERT语句执行速度和HINT /*+ append */及/*+ append nologging */的使用
    oracle查看表占用磁盘空间
  • 原文地址:https://www.cnblogs.com/wxquare/p/6912105.html
Copyright © 2011-2022 走看看