LeetCode OJ-- Text Justification
https://oj.leetcode.com/problems/text-justification/
细节题
class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L) {
vector<string> ans; if(L == )
{
ans.push_back("");
return ans;
} int index = ;
int begin = ;
int tempLen = ;
int end = ;
int spaces = ;
int extraSpace = ;
while(index < words.size())
{
begin = index;
tempLen = words[index].size();
index++;
while(index < words.size() && (tempLen + + words[index].size()) <= L)
{
tempLen = tempLen + + words[index].size();
index++;
}
end = index - ;
string str;
// not the last line
if(end != words.size() - )
{
// has more than one word
if(end != begin)
{
spaces = (L - tempLen) / (end - begin);
extraSpace = (L - tempLen) % (end - begin);
string strSpace;
for(int i = ; i < spaces + ; i++)
strSpace.append(" ");
for(int p = begin; p < end; p++)
{
str.append(words[p]);
str.append(strSpace);
if(p - begin < extraSpace)
str.append(" ");
}
str.append(words[end]);
}
else
{
str.append(words[begin]);
while(str.size() < L)
str.append(" ");
}
}
else
{
for(int p = begin; p < end; p++)
{
str.append(words[p]);
str.append(" ");
}
str.append(words[end]);
while(str.size() < L)
str.append(" ");
}
ans.push_back(str);
}
return ans;
}
};
LeetCode OJ-- Text Justification的更多相关文章
- LeetCode OJ——Text Justification
http://oj.leetcode.com/problems/text-justification/ 编译代码要看warnings!它提供了可能出问题的情况,比如类型转换上unsigned int ...
- 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 ...
- 【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 ...
- 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 ...
- [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] 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 @ Python
原题地址:https://oj.leetcode.com/problems/text-justification/ 题意: Given an array of words and a length L ...
随机推荐
- static关键字 详解
原文地址:http://blog.csdn.net/keyeagle/article/details/6708077 google了近三页的关于C语言中static的内容,发现可用的信息很少,要么长篇 ...
- astyle 使用说明 —— 集成到开发平台中
转自:https://www.cnblogs.com/jiangxinnju/p/4908575.html 欢迎关注我的社交账号: 博客园地址: http://www.cnblogs.com/jian ...
- stm32之PWM博客好文收藏
https://www.cnblogs.com/jiwangbujiu/p/5616376.html STM32F103 使用TIM3产生四路PWM https://www.cnblogs.com/c ...
- [Poj3281]Dining(最大流)
Description 有n头牛,f种食物,d种饮料,每头牛有nf种喜欢的食物,nd种喜欢的饮料,每种食物如果给一头牛吃了,那么另一个牛就不能吃这种食物了,饮料也同理,问最多有多少头牛可以吃到它喜欢的 ...
- 17,基于scrapy-redis两种形式的分布式爬虫
redis分布式部署 1.scrapy框架是否可以自己实现分布式? - 不可以.原因有二. 其一:因为多台机器上部署的scrapy会各自拥有各自的调度器,这样就使得多台机器无法分配start_urls ...
- 56、使用android studio(v1.3.*)修改包名 (rename package name)
一.修改包名 ①选中目录,开始构造 在弹窗中选中Rename directory 在弹窗中选中Rename package 填写新的包名,点击Refactor 如果有警告,不用管它,直接点击Do Re ...
- 【Luogu P2781】 传教
这题是可以用线段树做的. 虽然$n\leq 10^9$ 可以发现,真正需要用到的节点很少,故动态开点,只有需要用到的时候才新建节点. 这里我在下放标记的时候新建节点,因为每操作/查询一个节点都需要先下 ...
- LeetCode661图片平滑器
题目描述:包含整数的二维矩阵 M 表示一个图片的灰度.你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) ,平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元格不足八个, ...
- Visual C++ 经典的人脸识别算法源代码
说明:VC++ 经典的人脸识别算法实例,提供人脸五官定位具体算法及两种实现流程. 点击下载
- [报错处理]Could not find a version that satisfies the requirement xml (from versions)
安装xml库发生报错 pip3 install xml Collecting xml Could not find a version that satisfies the requirement x ...