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 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.
Corner Cases:
- 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.
分析:这一题需要注意两个点,a、当该行只放一个单词时,空格全部在右边 b、最后一行中单词间只有一个空格,其余空格全部在右边。然后只要贪心选择,在一行中尽量放多的单词。 本文地址
class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vector<string>res;
int len = words.size(), i = 0;
while(i < len)
{
//if(words[i].size() == 0){i++; continue;}
int rowlen = 0, j = i;
while(j < len && rowlen + words[j].size() <= L)
rowlen += (words[j++].size() + 1);
//j-i是该行放入单词的数目
if(j - i == 1)
{//处理放入一个单词的特殊情况
res.push_back(words[i]);
addSpace(res.back(), L - words[i].size());
i = j; continue;
}
//charaLen是当前行字母总长度
int charaLen = rowlen - (j - i);
//平均每个单词后的空格,j < len 表示不是最后一行
int meanSpace = j < len ? (L - charaLen) / (j - i - 1) : 1;
//多余的空格
int leftSpace = j < len ? (L - charaLen) % (j - i - 1) : L - charaLen - (j - i -1);
string tmp;
for(int k = i; k < j - 1; k++)
{
tmp += words[k];
addSpace(tmp, meanSpace);
if(j < len && leftSpace > 0)
{
tmp.push_back(' ');
leftSpace--;
}
}
tmp += words[j - 1];//放入最后一个单词
if(leftSpace > 0)addSpace(tmp, leftSpace); //对最后一行
res.push_back(tmp);
i = j;
}
return res;
} void addSpace(string &s, int count)
{
for(int i = 1; i <= count; i++)
s.push_back(' ');
}
};
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3475275.html
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 @ Python
原题地址:https://oj.leetcode.com/problems/text-justification/ 题意: Given an array of words and a length L ...
- 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 文本对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- [LeetCode] Text Justification words显示的排序控制
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- 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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- LeetCode OJ——Text Justification
http://oj.leetcode.com/problems/text-justification/ 编译代码要看warnings!它提供了可能出问题的情况,比如类型转换上unsigned int ...
- [LeetCode] 68. Text Justification 文本对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
随机推荐
- android textview 设置text 字体
1.使用不同的字库 mLocalClock.setTypeface(Typeface.SANS_SERIF); Typeface face = Typeface.createFromAsset(get ...
- [Weblogic]如何清理缓存
背景:在开发调试或测试时,很多时候重新更新部署服务后会发现某些更新并没有体现到,还是之前的情况,也或者会出现其他错误问题,这个时候就要考虑到可能是weblogic缓存未清理引起. 清理缓存步骤如下: ...
- 使用网站processon在线作图
网站:https://www.processon.com/ 该网站提供多种图形的在线制作,并支持多人协作. 目前提供以下图形的制作: 个人管理界面:
- sql server中游标
参考:http://blog.csdn.net/luminji/article/details/5130004 利用SQL Server游标修改数据库中的数据 SQL Server中的UPDATE语句 ...
- Backbone模型
现在进入最关键的组件 - 模型.模型用来存储应用的所有数据,以及直接和数据操作相关的逻辑.Backbone中的模型类是Backbone.Model,它包含了数据存储,数据验证,以及数据发生变动时触发相 ...
- 获取某地的经纬度 && 通过经纬度获取相应的地理位置
最近要通过一个经纬度判断该经纬度是否位于某个地区内,所以通过网上查找资料,整合后出了下面的内容. 1.通过地址获取改地址的经纬度 /** * @param addr * 查询的地址 * @return ...
- D_S 线性表的顺序表示和实现
线性表的顺序表示又称为顺序存储结构或顺序映像 顺序存储定义:把逻辑上相邻的数据元素存储在物理上相邻的存储单元中的存储结构,简言之,逻辑上相邻,物理上也相邻 顺序存储方法:用一组地址连续的存储单元依次存 ...
- [转载]iTOP-4412开发板搭建最小linux系统
本文转迅为电子论坛:http://www.topeetboard.com 最小linux系统所需资料下载:http://pan.baidu.com/s/1kTNan0j 开发板不仅可以运行Androi ...
- [转]轻松学习Ionic (四) 修改应用图标及添加启动画面(更新官方命令行工具自动生成)
本文转自:http://blog.csdn.net/zapzqc/article/details/42237935 由于Ionic更新了命令行工具,以后修改应用图标和添加启动画面就简单了,最新方法见最 ...
- gnuplot 学习笔记
1 如何运行 gnuplot是一个命令行输入的工具,把命令行写入一个文本file1 file2.使用下列方式运行. gnuplot {option} file1 file2 2 产生一个图标,不管数据 ...