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 ...
随机推荐
- windows 编程中的常见bug
错误 1 : error LNK2001: 无法解析的外部符号 _WTSQueryUserToken@8 解决办法: ——>查看链接器->输入->附加依赖项,依照debug模 ...
- 关于移动端click事件绑定的一个细节
click是最常见的点击事件,但是对于移动终端,比如手机,一般都是以touch事件代替的,而click事件在手机也是生效的,只是会有1-2秒左右的延迟,那么当你想要用click而非touch事件的时候 ...
- svn conflict
安装svn apt-get install subversion 当前两个人都更新版本为version1 A修改了monitor.txt文件 提交后版本为version2 B也修改了monitor.t ...
- 编译hadoop遇到maven timeout
在编译hadoop的过程中,使用ant jar进行编译时,提示maven版本库连接超时的问题,通过搜索发现,在如下文件的位置中有repo2的版本库地址,这个地址在国内,目前不能正常的访问: 将 ...
- SQL Server同步复制问题排查方法
1.应用复制的命令时在订阅服务器上找不到该行 解决方法:用系统存储过程sp_browsereplcmds(返回分发数据库中存储的可读版本复制命令的结果集,并将其用作诊断工具. 此存储过程在分发服务器上 ...
- 软件测试作业1--描述Error
记忆犹新的错误: 上个学期选修了可视化这门课程,最后大作业用d3实现,在使用d3读取csv数据的时候出现了以下Error: 我先是在代码中读取了某csv格式的数据,并且将其存入变量root中,然后对r ...
- Spring配置文件外部化配置及.properties的通用方法
摘要:本文深入探讨了配置化文件(即.properties)的普遍应用方式.包括了Spring.一般的.远程的三种使用方案. 关键词:.properties, Spring, Disconf, Java ...
- Linux 常用命令行
Linux常用命令行 第一部分: cd命令 第二部分:文件操作 第三部分:压缩包操作
- ZOJ 3865 Superbot(优先队列--模板)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5477 主要思路:1.从一个点(cur)到它相邻的点(next),所需 ...
- Linux 系统常用命令汇总(二) vi 文本编辑
文本编辑 vi 命令 作用 +文件名 编辑文本文件,若文件不存在同时创建该文件 Ctrl+f 向后翻一页 Ctrl+b 向前翻一页 Ctrl+d 向后翻半页 Ctrl+u 向前翻半页 + 光标移动到下 ...