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. easyhook报错The given 64-Bit library does not exist

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

  2. Servlet的自动加载

    Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 /* Style Definiti ...

  3. 软件结构B/S和C/S

    C/S(Client  Server)结构的软件: 比如: QQ. 极品飞车. 飞信 . 迅雷 缺点:更新的时候需要用户下载更新包然后再安装,程序员则需要开发客户端与服务端. 优点: 减轻服务端的压力 ...

  4. Jmeter(九)集合点

    性能测试需要模拟大量用户并发,集合点能够尽量让虚拟用户同一时刻发送请求, 在Jmeter中集合点是通过定时器-同步定时器来完成的.

  5. linux(centOS7)的基本操作(七) 其它

    本地与linux服务器之间的文件传输 本地下载的文件,如果想在远端的linux服务器上执行,需要文件传输.如果本地使用windows系统,则借助XFTP软件的图形界面即可.如果本地使用macOS系统, ...

  6. Python_基础知识储备

    目录 目录 前言 初识Python 解析型与编译型 OOP与POP 相关概念1 Python的解释器 Python程序设计的思想 Python的编程风格 最后 前言 前面的博文从记录了如何Setup ...

  7. Java编写时钟 Applet 程序

    简单分析: package clockApplet; import java.applet.Applet; import java.awt.Color; import java.awt.Graphic ...

  8. 阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类

    注解配置和xml的配置要实现的功能都是一样的.都是要降低程序间的耦合,只不过配置的形式不一样 打包方式改成jar 加入spring 的依赖 复制之前的代码过来 复制到我们新建的工程里 resurces ...

  9. 如何修改eclipse中的maven的仓库地址

    最近的有一个朋友问我如何修改eclipse的maven的本地仓库,我想了一下,这个玩意一般是不用修改的,主要是你本地安装的maven在哪个位置,一般的本地仓库位置在 C:\Users\username ...

  10. Web08_MySQL&JDBC回顾

    数据库操作:DATABASE 查看正在使用的数据库: SELECT DATABASE(); 表操作:TABLE 修改表修改列明 ALTER TABLE 表名 CHANGE 旧列名 新列名 类型(长度) ...