LeetCode_Text Justification
Given an array of words and a length L, format the text such that each line has exactly L 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 L 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 extra space is inserted between words. For example,
words: ["This", "is", "an", "example", "of", "text", "justification."]
L: 16. Return the formatted lines as: [
"This is an",
"example of text",
"justification. "
]
Note: Each word is guaranteed not to exceed L in length.
分析: 主要注意两个地方。一, 正常的句子单词之间的空格数目是一;二,最后一行句子按正常处理
class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<string> res;
if(words.size() < ) return res;
int start , end, len , evenSpace, bonus;
end = ;
while(true){
len = ;
for(start = end; end < words.size(); ++end){
if(len +(end - start) + words[end].length() > L) break;// normal sentence has one space between words
len += words[end].length();
}
end--;
if(end == start){ // only one word
string line = words[end];
line.append(L - len, ' ');
res.push_back(line);
}else{ // multiply words
evenSpace = (L - len)/(end - start);
bonus = L -len - evenSpace*(end - start);
bool isLastLine = (end == words.size() -);
if(isLastLine){
evenSpace = ;
bonus = ;
}
string line = words[start];
for(int i = start + ; i <= end; ++i)
{
int space = evenSpace;
if(bonus > )
{
--bonus;
++space;
}
line.append(space,' ');
line.append(words[i]);
}
if(isLastLine){
line.append(L-len - (end - start), ' ');
}
res.push_back(line);
}
end++;
if(end == words.size()) break;
}
return res;
}
};
LeetCode_Text Justification的更多相关文章
- [LeetCode] Text Justification 文本左右对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- 【leetcode】Text Justification
Text Justification Given an array of words and a length L, format the text such that each line has e ...
- 【leetcode】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
题目 Given an array of words and a length L, format the text such that each line has exactly L charact ...
- LeetCode:Text Justification
题目链接 Given an array of words and a length L, format the text such that each line has exactly L chara ...
- Java for LeetCode 068 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 (String Manipulation)
https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...
- juce Justification 分析
很简单的一个类,一个rect放置在另一个rect中如何放置.只是没有考虑边距等,估且认为是在外层作考虑吧.然后认为是外框比内框大,所以外层怕是要进行检查才行 #ifndef JUCE_JUSTIFIC ...
随机推荐
- 【HDOJ】1406 Ferry Loading III
模拟,注意需要比较队头与当前时间的大小关系. #include <cstdio> #include <cstring> #include <cstdlib> #de ...
- Linux系统编程(26)——守护进程
Linux系统启动时会启动很多系统服务进程,比如inetd,这些系统服务进程没有控制终端,不能直接和用户交互.其它进程都是在用户登录或运行程序时创建,在运行结束或用户注销时终止,但系统服务进程不受用户 ...
- 【转】在Ubuntu 12.04 上为Virtualbox 启用USB 设备支持--不错
原文网址:http://www.cnblogs.com/ericsun/archive/2013/06/10/3130679.html 虚拟机我一直在用,不是说离不开Windows,而是有些时候一些应 ...
- 测试Remoting三种信道Http,Tcp,Ipc和Web Service的访问速度 (转)
Remoting和Web Service是.net中的重要技术,都可用来实现分布式系统开发,如果是不同的平台就只能选择Web Service,但如果是同一平台,就都可以选择了.到底选择那种,当然还有访 ...
- MD中bitmap源代码分析--入题概述
在MD模块中,各级raid都使用的一份bitmap的源码,也就是说共用一种bitmap的流程,下面以raid1的使用为例来分析bitmap的工作原理. 在使用raid1磁盘阵列的时候,对于数据的可靠性 ...
- 关于Python中的for循环控制语句
#第一个:求 50 - 100 之间的质数 import mathfor i in range(50, 100 + 1): for j in range(2, int(math.sqrt(i)) ...
- 【转】Eclipse中创建并运行Servlet项目
最近看了写http协议的学习资料,因此想要实现下andriod平台和服务器通信,那就servlet吧,哎哟,还不错哦!顺便说下,我这个servlet服务是想获得用户名和密码进行校验,然后反给客户端状态 ...
- HDU 蟠桃记
蟠桃记 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submissio ...
- openwrt上网配置的一些理解(四)
这次要解决的问题是3g上网和wan口上往可以随意切换,当然能够叠加也是好事,不过这不是我关心的.下面还是修改3个文件network,firewall,multiwan.首先在network中加入界面配 ...
- -bash: mysql: command not found 解决办法 (转)
root@DB-02 ~]# mysql -u root-bash: mysql: command not found 原因:这是由于系统默认会查找/usr/bin下的命令,如果这个命令不在这个目录下 ...