68. Text Justification (JAVA)
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)的更多相关文章
- leetcode@ [68] Text Justification (String Manipulation)
https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...
- [LeetCode] 68. Text Justification 文本对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- 68. Text Justification
题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...
- 【一天一道LeetCode】#68. Text Justification
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [leetcode]68. Text Justification文字对齐
Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...
- 68. Text Justification *HARD*
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- 【LeetCode】68. Text Justification
Text Justification Given an array of words and a length L, format the text such that each line has e ...
- 68. Text Justification一行单词 两端对齐
[抄题]: Given an array of words and a width maxWidth, format the text such that each line has exactly ...
- Leetcode#68 Text Justification
原题地址 没有复杂的算法,纯粹的模拟题 先试探,计算出一行能放几个单词 然后计算出单词之间有几个空格,注意,如果空格总长度无法整除空格数,前面的空格长度通通+1 最后放单词.放空格,组成一行,加入结果 ...
随机推荐
- ES6 方法函数
1.模块的整体加载 除了指定加载某个输出值,还可以使用整体加载,即用星号(*)指定一个对象,所有输出值都加载在这个对象上面. 下面是一个circle.js文件,它输出两个方法area和circumfe ...
- Running .sh scripts in Git bash
Running .sh scripts in Git bash Let's say you have a script script.sh. To run it (using Git Bash), y ...
- spark streaming 2: DStream
DStream是类似于RDD概念,是对数据的抽象封装.它是一序列的RDD,事实上,它大部分的操作都是对RDD支持的操作的封装,不同的是,每次DStream都要遍历它内部所有的RDD执行这些操作.它可以 ...
- spark 2015 发展方向
https://www.youtube.com/watch?v=YWppYPWznSQ Databrick的spark 2015规划聚会的视频,看起来今年会在SQL和MLlib上发力多一点. ...
- PHP 封装类来访问数据库
封装访问数据库的类 <?php class DBDA { public $host = "localhost"; //数据库地址 public $uid = "ro ...
- 用xmmp+openfire+smack搭建简易IM实现
功能实现:注册,登录,单聊表情,文本,图片,语音的发送接收,添加好友,删除好友,查找好友,修改密码,消息提醒设置,获取离线消息等功能 1.前期准备 1.下载opnefire软件:https://www ...
- web开发(九) 使用javamail进行发送邮件,(使用QQ,163,新浪邮箱服务器)
在网上看见一篇不错的文章,写的详细. 以下内容引用那篇博文.转载于<http://www.cnblogs.com/whgk/p/6506027.html>,在此仅供学习参考之用. 一.发送 ...
- Centos7 yum 源安装nginx
一.建立nginx源 vim /etc/yum.repos.d/nginx.repo [nginx]name=nginx repobaseurl=http://nginx.org/packages/c ...
- Day02:变量 / JAVA基本类型
变量 什么是变量? 变化数值的载体 变量声明.初始化.使用 声明变量 类型 大小(占用空间) int x; 初始化变量 (赋值) 给予占用空间 int x=35; 使用变量 直接使用变量名 注意: ...
- BeautifulSoup解析豆瓣即将上映的电影信息
工欲善其事,必先利其器,我们首先得了解beautifulsoup的使用,这其实是一个比较简单的东西 BeautifulSoup的基本使用语法规则 .find() 使用示例 soup.find('a ...