leetcode@ [68] Text Justification (String Manipulation)
https://leetcode.com/problems/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.
A line other than the last line might contain only one word. What should you do in this case?
In this case, that line should be left-justified.
class Solution {
public:
string func(int len) {
string res = "";
while(len--) res += " ";
return res;
}
vector<string> fullJustify(vector<string>& words, int maxWidth) {
vector<string> res; res.clear(); //Assuming that the length of longest string in the words must be shorter or equal to maxWidth.
int len = ;
int i = , size = words.size();
while(i < size) {
if(len + words[i].length() == maxWidth) {
len = ;
res.push_back(words[i]);
++i;
}
else if(len + words[i].length() < maxWidth) {
len = len + words[i].length();
int left = i;
while(i+ < words.size() && len++words[i+].length() <= maxWidth) {
len += +words[i+].length();
++i;
}
len = ;
int right = i, tot_len = , tot_empty_len, each_empty_len, remain_empty_len; for(int k = left;k <= right; ++k) tot_len += words[k].length(); tot_empty_len = maxWidth - tot_len;
each_empty_len = (left == right)? tot_empty_len: tot_empty_len / (right-left);
remain_empty_len = tot_empty_len - each_empty_len * (right-left); string r = "";
if(left == right) {
string tmp = func(tot_empty_len);
r += words[left] + tmp;
res.push_back(r);
}
else {
string tmp;
if(right <= size - ) {
for(int k = left; k <= right - ; ++k) {
if(remain_empty_len > ) {
tmp = func(each_empty_len+);
remain_empty_len--;
}
else tmp = func(each_empty_len);
r += words[k] + tmp;
}
r += words[right];
res.push_back(r);
}
else {
for(int k = left; k <= right - ; ++k) {
tmp = func();
r += words[k] + tmp;
}
r += words[right];
if(r.length() < maxWidth) r += func(maxWidth - r.length());
res.push_back(r);
}
}
++i;
}
}// end while return res;
}
};
leetcode 68: Text Justification
leetcode@ [68] Text Justification (String Manipulation)的更多相关文章
- [LeetCode] 68. Text Justification 文本对齐
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 width maxWidth, format the text such that each line has exactly maxWid ...
- Leetcode#68 Text Justification
原题地址 没有复杂的算法,纯粹的模拟题 先试探,计算出一行能放几个单词 然后计算出单词之间有几个空格,注意,如果空格总长度无法整除空格数,前面的空格长度通通+1 最后放单词.放空格,组成一行,加入结果 ...
- 【一天一道LeetCode】#68. Text Justification
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【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 length L, format the text such that each line has exactly L charac ...
- LeetCode OJ——Text Justification
http://oj.leetcode.com/problems/text-justification/ 编译代码要看warnings!它提供了可能出问题的情况,比如类型转换上unsigned int ...
- 【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 ...
随机推荐
- 学点PYTHON基础的东东--数据结构,算法,设计模式---访问者模式
说实话,感觉不是特别多,可能没遇到过多场面, 所以对应用场景没感觉吧. 反正,各种模式就是把类的实例传来传去,久而久之,产生了一些规律...:) # 轮子,引擎, 车身这些定义好了都不需要变动 cla ...
- 常用的富文本框插件FreeTextBox、CuteEditor、CKEditor、FCKEditor、TinyMCE、KindEditor ;和CKEditor实例
http://www.cnblogs.com/cxd4321/archive/2013/01/30/2883078.html 目前市面上用的比较多的富文本编辑器有: FreeTextBox 一个有很多 ...
- WCF之各种WCF引用方式
写在开头:本文内容来自 WCF全面解析中的一个经典例子,如果你已经看过了,那么可以忽略本文,本文旨在和大家分享不一样的WCF使用方法. 准备工作: 1.创建解决方案WCFService(当然名字可以任 ...
- UnsupportedClassVersionError: Bad version number in...
在使用eclipse开发servlet可能会出现一个很麻烦事情,版本不一致错误. java.lang.UnsupportedClassVersionError: Bad version number ...
- 【剑指offer】找出数组中出现一次的两个数
2013-09-08 10:50:46 一个整型数组中,除了两个数字之外,其他数字都出现了2次,找出这两个只出现一次的数字,要求时间复杂度是O(N),空间复杂度是O(1). 小结: 任何数与0异或,结 ...
- ogg实现oracle到sql server 2005的同步
一.源端(oracle)配置1.创建同步测试表create table gg_user.t01(name varchar(20) primary key);create table gg_user.t ...
- Windows Phone 获取网络类型(GSM/CDMA/WIFI/Ethernet)
一.判断是否有网络数据连接: 最基本的网络状态判断,如果没有网络连接,一切操作都进行不下去啦. Microsoft.Phone.Net.NetworkInformation.NetworkInterf ...
- [POJ 2429] GCD & LCM Inverse
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10621 Accepted: ...
- centos nginx 多端口配置过程记录
1. 编辑 /usr/local/nginx/vhosts/ 在此目录下增加一文件,如;ci.ainux.com,或复制一个文件 修改其中的端口和目录,更改log_format 名称 重启nginx ...
- 装饰器模式(Decorator)
转自http://blog.csdn.net/hust_is_lcd/article/details/7884320 1.认识装饰器模式 装饰模式能够实现动态的为对象添加功能,是从一个对象外部来给对象 ...