zoukankan      html  css  js  c++  java
  • [LeetCode] 68. Text Justification

    Given an array of strings words and a width maxWidth, format the text such that each line has exactly maxWidth 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 maxWidth characters.

    Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line does 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.

    Note:

    • A word is defined as a character sequence consisting of non-space characters only.
    • Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
    • The input array words contains at least one word.

    Example 1:

    Input: words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16
    Output:
    [
       "This    is    an",
       "example  of text",
       "justification.  "
    ]

    Example 2:

    Input: words = ["What","must","be","acknowledgment","shall","be"], maxWidth = 16
    Output:
    [
      "What   must   be",
      "acknowledgment  ",
      "shall be        "
    ]
    Explanation: Note that the last line is "shall be    " instead of "shall     be", because the last line must be left-justified instead of fully-justified.
    Note that the second line is also left-justified becase it contains only one word.

    Example 3:

    Input: words = ["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"], maxWidth = 20
    Output:
    [
      "Science  is  what we",
      "understand      well",
      "enough to explain to",
      "a  computer.  Art is",
      "everything  else  we",
      "do                  "
    ]

    Constraints:

    • 1 <= words.length <= 300
    • 1 <= words[i].length <= 20
    • words[i] consists of only English letters and symbols.
    • 1 <= maxWidth <= 100
    • words[i].length <= maxWidth

    文本左右对齐。

    给定一个单词数组和一个长度 maxWidth,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本。

    你应该使用“贪心算法”来放置给定的单词;也就是说,尽可能多地往每行中放置单词。必要时可用空格 ' ' 填充,使得每行恰好有 maxWidth 个字符。

    要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数。

    文本的最后一行应为左对齐,且单词之间不插入额外的空格。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/text-justification
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    这道题没有涉及算法概念,不过很考验编程功底。这道题就是实现 word 里的左右对齐功能,需要注意如下几点。

    • 每行尽可能多放单词
    • 单词之间的距离至少是一个空格
    • 如果有多余的空格但是不足以再多放一个单词的话,把空格均匀分配到每两个单词中间
    • 最后一行需要左对齐,用空格补足后面

    其余的请参见注释。

    时间O(n)

    空间O(n) - StringBuilder

    Java实现

     1 class Solution {
     2     public List<String> fullJustify(String[] words, int maxWidth) {
     3         // 记录结果
     4         List<String> res = new ArrayList<>();
     5         int n = words.length;
     6         int index = 0;
     7         while (index < n) {
     8             // totalChars - 当前行上的字母数量
     9             int totalChars = words[index].length();
    10             // 下一个单词
    11             int last = index + 1;
    12             while (last < n) {
    13                 // 试着多加一个单词 看当前行是否能塞得下 不行就break
    14                 if (totalChars + 1 + words[last].length() > maxWidth) {
    15                     break;
    16                 }
    17                 // 计算当前行的字母数
    18                 totalChars += 1 + words[last].length();
    19                 last++;
    20             }
    21             // 计算每行有几个gap 此时的last应该是下一行第一个单词的index
    22             int gaps = last - index - 1;
    23             StringBuilder sb = new StringBuilder();
    24             // 如果是最后一行
    25             if (last == n || gaps == 0) {
    26                 // 加入单词和空格
    27                 for (int i = index; i < last; i++) {
    28                     sb.append(words[i]);
    29                     sb.append(' ');
    30                 }
    31                 // 去掉最后一个单词之后的空格
    32                 // 这一行是有必要的 尤其是如果最后一行存在的单词数量 + 空格正好等于maxWidth的情况
    33                 sb.deleteCharAt(sb.length() - 1);
    34                 // 之后补足空格
    35                 while (sb.length() < maxWidth) {
    36                     sb.append(' ');
    37                 }
    38             } else {
    39                 // 开始拼接当前行的单词们
    40                 // space for each gap
    41                 int spaces = (maxWidth - totalChars) / gaps;
    42                 // extra spaces on each line
    43                 int rest = (maxWidth - totalChars) % gaps;
    44                 for (int i = index; i < last - 1; i++) {
    45                     sb.append(words[i]);
    46                     sb.append(' ');
    47                     for (int j = 0; j < spaces + (i - index < rest ? 1 : 0); j++) {
    48                         sb.append(' ');
    49                     }
    50                 }
    51                 sb.append(words[last - 1]);
    52             }
    53             res.add(sb.toString());
    54             index = last;
    55         }
    56         return res;
    57     }
    58 }

    LeetCode 题目总结

  • 相关阅读:
    docker搭建本地仓库并制作自己的镜像
    docker命令及操作
    从零开始学android开发-项目打包发布
    从零开始学android开发-adt-bundle-eclipse下的修改android app名称
    从零开始学android开发-项目重命名
    Android Studio系列教程一--下载与安装
    Axure RP 7.0注册码
    MVC网站发布常见问题
    无间断滚动的新闻文章列表
    @HTML
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13326971.html
Copyright © 2011-2022 走看看