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. redis哨兵集群搭建

    下载redis jar包redis-4.0.11.tar.gz放在/data/redis目录下 解压 命令:tar -zxvf redis-4.0.11.tar.gz 解压后如图所示 在/usr/lo ...

  2. 字面常量 kotlin(2)

    字面常量数值常量字面值有以下几种:十进制: 123Long 类型用大写 L 标记: 123L十六进制: 0x0F二进制: 0b00001011注意: 不支持八进制Kotlin 同样支持浮点数的常规表示 ...

  3. easyhook报错The given 64-Bit library does not exist

    在调用 RemoteHooking.Inject 时,报错 查看easyhook源代码,出错位置如下 if(!RtlFileExists(UserLibrary)) { #ifdef _M_X64 T ...

  4. celery 启动命令

    celery 任务启动命令  celery worker -A  _tasks.tasks -l info -E tasks 就是celery 任务的文件 celery beat启动命令  celer ...

  5. ControlTemplate in WPF —— Menu

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...

  6. zabbix日志报错解决

    [root@bogon ldap]# cat /tmp/zabbix_server.log 9135:20181204:085433.351 using configuration file: /us ...

  7. Struts---多文件上传、单文件下载

    struts.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUB ...

  8. 用DotNetDetour HOOK .net类库

    https://github.com/bigbaldy1128/DotNetDetour ------------------------------------------------------- ...

  9. Ubuntu虚拟机共享文件夹的1234

    第一 在虚拟机内添加路径 进入虚拟机软件,点开工具栏上方虚拟机,点击设置,选择选项,查看共享文件夹,点击添加,下一步 第二: 第三 点击启用此共享 点击完成 第四 查看共享的文件 在mnt里可以看到S ...

  10. magento form.html不显示 window 和 Linux下的区别

    window 无大小写区别,所以可以显示表框 Linux 大小写敏感,显示不了 \app\code\community\Company\BabyPay\Model\Payment.php 里定义了fo ...