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 };
  • 相关阅读:
    [nginx]简单内容管理系统的spring项目的nginx.conf配置文件的注释理解
    [日常填坑]部署使用Idea开发的spring框架的多模块项目到服务器
    [日常填坑]centos7.0+版本服务器安装jdk9或者jdk10
    [日常填坑]centos7.0+版本服务器安装mysql
    机器学习实战之kNN算法
    机器学习实战之机器学习主要任务
    模式字符串匹配问题(KMP算法)
    ubuntu 更新引导命令
    ubuntu 14.04 中找不到 libgtk-x11-2.0.so
    maven project 更新总是jre-1.5
  • 原文地址:https://www.cnblogs.com/wxquare/p/6912105.html
Copyright © 2011-2022 走看看