【一天一道LeetCode】#68. Text Justification
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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 Lcharacters.
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.
(二)解题
题目大意:按照一定的格式对文本进行对齐。
需要注意以下几点:
1、只有一个单词直接在后面补空格,如“a”,5 输出“a ”
2、最后一组单词不需要对齐,如“a”,“b” 5 输出“a b ”
3、单词与单词之间至少要有一个空格隔开
具体 思路见代码注释:
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
vector<string> ret;//返回值
int width = words[0].size();
int num = 1;
int last = 0 ;//纪录每一次maxWidth的起始序号
for(int i = 1 ; i<words.size() ; i++)
{
width+=words[i].size();
num++;
if(width+num-1>maxWidth)//这里要注意每个单词之间要用空格隔开
{
width-=words[i].size();//清除掉最后一个数
num--;
string temp;
if(num==1){//只有一个单词的情况
temp+=words[i-1];
while(temp.size()!=maxWidth) temp+=" ";//在后面补齐空格
ret.push_back(temp);
}
else//多个单词,但不是结尾的情况
{
int blankWidth = maxWidth - width;
int gap = 0;
for(int j = last ; j < i ; j++)
{
if(j==i-1) gap=0;//最后一个单词后面不加空格
else {
gap = blankWidth/(num-1);//每一次进来都要算需要增加多少空格
gap = blankWidth%(num-1)>0?gap+1:gap;//保证均匀分布
blankWidth -=gap;
}
temp+=words[j];
while(gap>0&&gap--) temp+=" ";
num--;
}
ret.push_back(temp);
}
//初始化下一个循环
last=i;
width = words[i].size();
num=1;
}
}
if(last<words.size()){//考虑末尾不足maxWidth的情况
int j = last;
string temp;
while(j<words.size()){//先按一个空格添加words
temp+=words[j++];
if(temp.size()<maxWidth) temp+=" ";
}
while(temp.size()<maxWidth) temp+=" ";//最后不足maxWidth就用空格补足
ret.push_back(temp);
}
return ret;
}
};
【一天一道LeetCode】#68. Text Justification的更多相关文章
- 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]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
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 ...
随机推荐
- delphi 线程教学第四节:多线程类的改进
第四节:多线程类的改进 1.需要改进的地方 a) 让线程类结束时不自动释放,以便符合 delphi 的用法.即 FreeOnTerminate:=false; b) 改造 Create 的参数 ...
- Bootstrap3 表格-状态类
通过这些状态类可以为行或单元格设置颜色. .active---鼠标悬停在行或单元格上时所设置的颜色 .success--–标识成功或积极的动作 .info----标识普通的提示信息或动作 .warni ...
- MAC OS X下的Linux环境
关键字: HomeBrew,好比Windows下的Cygwin 安装Homebrew 该si胜过macport ruby -e "$(curl -fsSL https://raw.githu ...
- CentOS7下安装GitLab
三步在CentOS7系统下,完成GitLab的安装. 1.安装和配置必须的依赖 sudo yum install curl policycoreutils openssh-server openssh ...
- [CSDN_Markdown]使用LaTeX基本数学公式
简介 以前我很头疼在博文里写公式,一直期盼CSDN的博文编辑器能支持LaTeX 公式输入,今天终于可以使用这个功能了!此文主要讨论如何在CSDN的Markdown编辑器中写 LaTeX 公式! 使用L ...
- OpenCV Python 录制视频
调用摄像头 引入库支持 初始化 调整界面大小 实时显示 录制视频并保存 fourcc问题解决 代码实现 效果展示 总结 学到实用OpenCV调用笔记本电脑的摄像头,并录制视频保存到本地硬盘的时候,出现 ...
- gloox配置聊天室
gloox配置聊天室 (金庆的专栏) gloox是XMPP协议的C++客户端库.以下代码测试创建多人聊天室(MUC), 并进行配置.参照gloox中的muc示例代码.gloox代码示例中没有聊天室的配 ...
- Racket 模拟SICP的流(延时计算)
默认的Racket是要对函数参数进行求值的, 例如(f 1 (+ 1 2))里面,(+ 1 2)要先求值为3,变为(f 1 3)再进行下一步操作.因此, Racket若按照SICP使用define关键 ...
- JAVA面向对象-----extends关键字
继承使用extends关键字实现 1:发现学生是人,工人是人.显然属于is a 的关系,is a就是继承. 2:谁继承谁? 学生继承人,发现学生里的成员变量,姓名和年龄,人里边也都进行了定义.有重 复 ...
- Hive-ORC文件存储格式
ORC文件格式是从Hive-0.11版本开始的.关于ORC文件格式的官方文档,以及基于官方文档的翻译内容这里就不赘述了,有兴趣的可以仔细研究了解一下.本文接下来根据论文<Major Techni ...