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. JSP——指令(Directive)

    指令是一种JSP句法元素,它告诉JSP转换器应该如何将某个JSP页面转换成Servlet. JSP 2.2中定义了几个指令:page.include.taglib.tag.attribute以及var ...

  2. spark streaming 3: Receiver 到 submitJobSet

     对于spark streaming来说,receiver是数据的源头.spark streaming的框架上,将receiver替换spark-core的以磁盘为数据源的做法,但是数据源(如监听某个 ...

  3. nginx-location正则表达式匹配规则及动静分离

    nginx-location正则表达式匹配规则及动静分离  发表于 2018年03月5日 |  分类于 nginx|  0 nginx,location常用正则表达式,及nginx动静分离 nginx ...

  4. 五一 DAY 3

    DAY 3      2019.4.30 动态规划DP Dp是一个很抽象的东西 方法没有明显区别,很难总结套路 啥是DP? DP等价于DAG!!! (1)无后效性:DP的所有状态之间组成一个DAG ( ...

  5. ERROR:非静态成员引用必须与特定对象相对

    非静态成员引用必须与特定对象相对 引用非静态成员前应该先声明该类的对象 typedef struct _SUPERRESOLUTIONPARAM { int Times; //重建倍数,指的是分别对长 ...

  6. fiddler抓取app的https的包

    线上问题的排查有时候需要抓包,但是是https协议的,则需要安装证书 在Android 6.0 (API level 23)及以前,APP默认信任系统自带的CA证书以及用于导入的CA证书,Androi ...

  7. Elasticsearch 为何要在 7.X版本中 去除type 的概念

    背景说明 Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎.无论在开源还是专有领域,Lucene可以被认为是迄今为止最先进.性能最好的.功能最全的搜索引擎库. El ...

  8. 关机报 at-spi-bus-launcher

    查看是否有autostart文件夹,有则不建立 mkdir -v ~/.config/autostart 可直接修改/etc/xdg/autostart/at-spi-dbus-bus.desktop ...

  9. NFA转换为等价的DFA

    在编译系统中,词法分析阶段是整个编译系统的基础.对于单词的识别,有限自动机FA是一种十分有效的工具.有限自动机由其映射f是否为单值而分为确定的有限自动机DFA和非确定的有限自动机NFA.在非确定的有限 ...

  10. 【github】git常用命令和node项目

    这是之前在本地搞的一个小东西.因为之前电脑经常会很卡.后来发现任务管理器中多了两个不知名的进程:chromgpu.exe和flrefox.exe 乍一看以为和chrome和firefox两个浏览器有关 ...