【LeetCode】68. Text Justification

栏目: 编程工具 · 发布时间: 8年前

内容简介:【LeetCode】68. Text Justification

问题描述

https://leetcode.com/problems/text-justification/#/description

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 L 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.

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.

算法

文本显示调整问题,将一系列单词 words[n] 按行显示,每一行做多只能有 L 个字母,如: words=["This", "is", "an", "example", "of", "text", "justification."],L=16

最终显示的行为: ["This is an", "example of text", "justification. "]

输出的行需要满足几个条件

  1. 每一行的单词要尽可能的多,并且要左右都能顶格,不足 L 个字符时,可以使用空格 ' ' 代替字母
  2. 每一行单词之间的空格要尽可能地均匀,不能满足均匀的前提下,左边的要比右边的多
  3. 最后一行只要满足左边顶格,并且单词之间不要有多余空格

算法:

遍历到一个单词时,往前探到几个单词符合要求,即单词的字母数之和加上单词之间的空格要小于等于 L ,探到符合要求的单词数后就开始组织行。

因为单词已经有了,接下来就是安排好单词的位置了,有 n 个单词,那么就会有 n-1 个间隔,那么除了最后一行外的每个间隔的空格数应该是: (L-n个单词的字母总量)/(n-1) ,但因为有时不能除尽,最后一个间隔有可能会少,要进行判断。最后一行按照普通的做法,从左到右排好单词即可。

参考自: https://discuss.leetcode.com/topic/9147/simple-java-solution

代码

public List<String> fullJustify(String[] words, int maxWidth) {
            List<String> lines = new ArrayList<>();
            int cur = 0;
            while(cur < words.length) {
                int last = cur+1;
                int cnt = words[cur].length(); // 单词的字母数
                while(last < words.length) {
                    if(words[last].length() + cnt + 1 > maxWidth) {
                        break;
                    }
                    cnt += words[last].length()+1;
                    last++;
                }
                int gap = last - cur - 1;
                StringBuilder sb = new StringBuilder();
                if(last == words.length || gap == 0) { // 到了最后一行或者只有一个单词
                    for(int i=cur;i<last;i++) {
                        sb.append(words[i]+" ");
                    }
                    sb.deleteCharAt(sb.length()-1);
                    for(int i=sb.length();i<maxWidth;i++) {
                        sb.append(" ");
                    }
                } else {
                    int spaces = (maxWidth - cnt) / gap;
                    int rem = (maxWidth - cnt) % gap;
                    for(int i=cur;i<last;i++) {
                        sb.append(words[i]);
                        if(i<last-1) {
                            for(int j=0;j<=(spaces+(i-cur<rem?1:0));j++) {
                                sb.append(" ");
                            }
                        }
                    }
                }
                lines.add(sb.toString());
                cur = last;
            }
            return lines;
        }
转载请注明出处

http://www.zgljl2012.com/leetcode-68-text-justification/


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

C++ Primer 中文版(第 4 版)

C++ Primer 中文版(第 4 版)

Stanley B.Lippman、Josée LaJoie、Barbara E.Moo / 李师贤、蒋爱军、梅晓勇、林瑛 / 人民邮电出版社 / 2006 / 99.00元

本书是久负盛名的C++经典教程,其内容是C++大师Stanley B. Lippman丰富的实践经验和C++标准委员会原负责人Josée Lajoie对C++标准深入理解的完美结合,已经帮助全球无数程序员学会了C++。本版对前一版进行了彻底的修订,内容经过了重新组织,更加入了C++ 先驱Barbara E. Moo在C++教学方面的真知灼见。既显著改善了可读性,又充分体现了C++语言的最新进展和当......一起来看看 《C++ Primer 中文版(第 4 版)》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

html转js在线工具
html转js在线工具

html转js在线工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具