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

注意对最后一行的单独考虑

class Solution {
public List<String> fullJustify(String[] words, int maxWidth) {
String s = "";
List<String> ret = new ArrayList<String>();
int curWidth = 0; //Width of current String
int start = 0; //start word index of current String
int spaceCnt, totalSpace, q, r, i, j, k;
for(i = 0; i < words.length; ){
if(s.length()==0){
s += words[i];
curWidth += words[i].length();
i++;
}
else if(curWidth + words[i].length() + 1 > maxWidth){
spaceCnt = i-1-start;
totalSpace = maxWidth - (curWidth-spaceCnt);
if(spaceCnt == 0){
for(j = 0; j < totalSpace; j++){
s += ' ';
}
}
else{
q = totalSpace/spaceCnt;
r = totalSpace%spaceCnt;
for(j = 0; j < r; j++){
for(k = 0; k <= q; k++){ //1 more space
s += ' ';
}
s += words[start+1+j];
}
for(j = r; j < spaceCnt; j++){
for(k = 0; k < q; k++){
s += ' ';
}
s += words[start+1+j];
}
} ret.add(s); //initialize
s = "";
curWidth = 0;
start = i;
}
else{
curWidth = curWidth + words[i].length() + 1;
i++;
}
} //deal with last case
for(i = start+1; i < words.length; i++){
s = s + ' ' + words[i];
}
for(i = curWidth; i < maxWidth; i++){
s += ' ';
}
ret.add(s); return ret;
} }

68. Text Justification (JAVA)的更多相关文章

  1. leetcode@ [68] Text Justification (String Manipulation)

    https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...

  2. [LeetCode] 68. Text Justification 文本对齐

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  3. 68. Text Justification

    题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...

  4. 【一天一道LeetCode】#68. Text Justification

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. [leetcode]68. Text Justification文字对齐

    Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...

  6. 68. Text Justification *HARD*

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  7. 【LeetCode】68. Text Justification

    Text Justification Given an array of words and a length L, format the text such that each line has e ...

  8. 68. Text Justification一行单词 两端对齐

    [抄题]: Given an array of words and a width maxWidth, format the text such that each line has exactly  ...

  9. Leetcode#68 Text Justification

    原题地址 没有复杂的算法,纯粹的模拟题 先试探,计算出一行能放几个单词 然后计算出单词之间有几个空格,注意,如果空格总长度无法整除空格数,前面的空格长度通通+1 最后放单词.放空格,组成一行,加入结果 ...

随机推荐

  1. JS 类和继承

    function User(name, pass) { this.name = name this.pass = pass } User.prototype.showName = function ( ...

  2. ajax 调用 webService

    前台脚本<script type="text/javascript"> $(function () { $.ajax({ type: "POST", ...

  3. 使用 apt-get 清理

    大多数使用基于 Debian 的系统的人通常会使用 apt-get 来安装软件包和升级,但是我们多久才清理一次?让我们看下该工具本身的一些清理选项. 在基于 Debian 的系统上运行 apt-get ...

  4. 方差分析(python代码实现)

    python机器学习-乳腺癌细胞挖掘(欢迎关注博主主页,学习python视频资源,还有大量免费python经典文章) https://study.163.com/course/introduction ...

  5. leetcode 115不同的子序列

    滚动数组: /***** 下标从1开始 dp[i][j]:= numbers of subseq of S[1:j] equals T[1:i] if(s[j]==t[i]):(那么之后的子串可以是是 ...

  6. jQuery.validator.addMethod自定义验证

    jQuery.validator.addMethod("numOrLetter", function(value, element) { return this.optional( ...

  7. maven pom.xml设置jdk编译版本为1.8

    <build> <finalName>myweb</finalName> <plugins> <!--JDK版本 --> <plugi ...

  8. MySQL 数据库架构

    1 架构 master-slave性能,qps,tps,cpu,disk,memory,network,raid卡,fashion卡, 并发量:同一时刻需要db处理 连接量: 大表:1000w行或者表 ...

  9. 手机连接电脑,使用adb命令

    手机连接电脑使用adb命令,主要是有2种方式,其中最常见的就是第一种,用usb连线使用 1:adb usb - restarts the adbd daemon listening on USB ad ...

  10. pycryptodom的源码安装

    1.去网站https://pypi.python.org/pypi/pycryptodome/#downloads下载 2.python setup.py build -> python set ...