资讯专栏INFORMATION COLUMN

[LeetCode] 68. Text Justification

Karrdy / 3086人阅读

Problem

Given an array of 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 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.

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                  "
]

Solution
class Solution {
    public List fullJustify(String[] words, int n) {
        List res = new ArrayList<>();
        if (words == null || words.length == 0) return res;
        for (String word: words) {
            if (word.length() > n) {
                throw new IllegalArgumentException("Invalid input.");
            }
        }
        
        int index = 0;
        while (index < words.length) {
            int len = words[index].length();
            int next = index+1;
            while (next < words.length) {
                if (len+1+words[next].length() > n) break;
                len += (1+words[next].length());
                next++;
            }
            StringBuilder sb = new StringBuilder();
            int count = next-1-index;
            
            //if reached last line, or still in same line of index
            if (next == words.length || count == 0) {
                for (int i = index; i < next; i++) {
                    sb.append(words[i]+" ");
                }
                //delete the last extra space
                sb.deleteCharAt(sb.length()-1);
                //fill the total length to n with white space
                for (int i = sb.length(); i < n; i++) {
                    sb.append(" ");
                }
            } else {
                int spaces = (n-len)/count;
                int remains = (n-len)%count;
                for (int i = index; i < next; i++) {
                    sb.append(words[i]);
                    if (i < next-1) {
                        for (int j = 0; j <= (spaces+((i-index) < remains ? 1 : 0)); j++) {
                            sb.append(" ");
                        }
                    }
                }
            }
            
            res.add(sb.toString());
            index = next;
        }
        
        
        return res;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/72275.html

相关文章

  • leetcode-68-Text Justification

    68. Text JustificationDescriptionHintsSubmissionsDiscussSolutionGiven 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) justi...

    remcarpediem 评论0 收藏0
  • Leetcode PHP题解--D68 283. Move Zeroes

    摘要:题目链接题目分析给定一个整数数组,将值为的元素移动到数组末尾,而不改动其他元素出现的顺序。再在去后的元素末尾填充到计算出的数组长度。最终代码若觉得本文章对你有用,欢迎用爱发电资助。 D68 283. Move Zeroes 题目链接 283. Move Zeroes 题目分析 给定一个整数数组,将值为0的元素移动到数组末尾,而不改动其他元素出现的顺序。 思路 计算总共有多少个元素。 再...

    xiongzenghui 评论0 收藏0
  • 13. 罗马数字转整数-----leetcode刷题(python解题)

    摘要:题目罗马数字包含以下七种字符,,,,,和。字符数值例如,罗马数字写做,即为两个并列的。通常情况下,罗马数字中小的数字在大的数字的右边。同样地,数字表示为。给定一个罗马数字,将其转换成整数。 [TOC] 题目 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。 字符 数值 I 1 V 5 X ...

    Gu_Yan 评论0 收藏0
  • 8.leetcode Self Dividing Numbers

    摘要:题目例子我的解法其他解法这个方法不用转化成字符串,直接得到的数再除 1. 题目 A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 ...

    付永刚 评论0 收藏0
  • 6leetcode機器移動

    1 題目 There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves. The move sequence is re...

    glumes 评论0 收藏0

发表评论

0条评论

Karrdy

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<