【一天一道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 ...
随机推荐
- Redis从入门到精通:中级篇
原文链接:http://www.cnblogs.com/xrq730/p/8944539.html,转载请注明出处,谢谢 本文目录 上一篇文章以认识Redis为主,写了Redis系列的第一篇,现在开启 ...
- 安卓Tv开发(一)移动智能电视之焦点控制(触控事件)
前言:移动智能设备的发展,推动了安卓另一个领域,包括智能电视和智能家居,以及可穿戴设备的大量使用,但是这些设备上的开发并不是和传统手机开发一样,特别是焦点控制和用户操作体验风格上有很大的区别,本系列博 ...
- 一小时入门PHP
[版权申明:本文系作者原创,转载请注明出处] 文章出处:[http://blog.csdn.net/sdksdk0/article/details/52332296](http://blog.csdn ...
- Leetcode难度表及解题汇总
Leetcode难度表及解题汇总 参考网上一份题目难度表,以及本人的解题. Id Question Difficulty Frequency Data Structures Algorithms Bl ...
- TCP的TIME_WAIT状态
主动关闭的Socket端会进入TIME_WAIT状态,并且持续2MSL时间长度,MSL就是maximum segment lifetime(最大分节生命期),这是一个IP数据包能在互联网上生存的最长时 ...
- win10+ubuntu双系统安装方案
网上有很多教程,大多是win7,win8的,我折腾了一天,今天终于都安装好了,折腾的够呛,很多人都说挺简单的,嗯其实的确很简单,很多人回复说安装不成功,很有可能就是电脑安全权限的问题,我用的是华硕的电 ...
- ROS(indigo) 语音工具 科大讯飞 百度 pocketsphinx julius rospeex 16.11.22更新 ROS中文语音
ROS语音工具汇总,目前先给出链接,只用过一些简单的命令. 中文语音: 参考链接:使用科大讯飞库 1 http://www.ncnynl.com/archives/201611/1069.html 2 ...
- activiti 任务节点 处理人设置
1.1.1. 前言 分享牛原创(尊重原创 转载对的时候第一行请注明,转载出处来自分享牛http://blog.csdn.net/qq_30739519) 我们在使用activiti 工作流引擎的时候, ...
- Java 8 新特性之 Lambda表达式
Lambda的出现就是为了增强Java面向过程编程的深度和灵活性.今天就来分享一下在Java中经常使用到的几个示例,通过对比分析,效果应该会更好. – 1.实现Runnable线程案例 其存在的意义就 ...
- 安卓TextView完美展示html格式代码
对于TextView展示html格式代码,最简单的办法就是使用textview.setText(Html.fromHtml(html));,即便其中有img标签,我们依然可以使用ImageGetter ...