LeetCode(68) 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 L characters.
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. ”
]
分析
给定一个字符串数组以及规定长度,按规则将其分组输出;
题目本身是不难的,主要是规则繁杂:
- 首先,输出以是否为末行分为两类;
- 对于非末行单词组,又以其包含的单词个数分为两类,一是单个单词,二是多个单词;
第一步,讨论非末行单词组合:
(1)若该组只包含一个单词,规定其左对齐,不足指定长度以空格填充;
(2)若该组包含count个单词,那么它有(count-1)个间隔,每个间隔放置一个空格;此时,求出不足指定长度需要的额外空格数目,extraSpace,每个单词间隔填充extra/(count-1)个空格;此时,若不整除那么前extra%(count-1)个间隔再次填充一个空格;
第二步,讨论末行单词组合:
(1)若只有一个单词,左对齐,不足指定长度以空格填充;
(2)若该组有count个单词,那么它有(count-1)个间隔,每个间隔放置一个空格;不足指定长度,末尾填充;
AC代码
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
if (words.empty())
return vector<string>();
vector<string> ret;
int sz = words.size();
/*sumLen记录当前字符串长度,count记录包含的单词个数*/
vector<string> tmp;
int sumLen = 0, count = 0;
for (int i = 0; i < sz; ++i)
{
/*判断是否可以添加一个字符串*/
if ((sumLen + words[i].length() + count) <= maxWidth)
{
/*满足要求,单词个数增一,保存*/
++count;
sumLen = sumLen + words[i].length();
tmp.push_back(words[i]);
continue;
}//if
else{
/*只有一个单词,左对齐*/
if (1 == count)
{
string str = tmp[0];
while (str.length() < maxWidth)
str += " ";
ret.push_back(str);
}//if
else{
string str = "";
/*计算多余的空格总数,每个间隔至少一个空格*/
int extraSpace = maxWidth - sumLen - count + 1;
/*每个间隔需再增加的间隔*/
int everySpace = extraSpace / (count - 1);
/*前间隔需要额外放置一个空格的间隔数*/
int frontSpace = extraSpace % (count - 1);
for (int k = 0; k < count - 1; ++k)
{
int j = 0;
while (j < everySpace + 1)
{
tmp[k] += " ";
++j;
}//while
}//for
/*前frontSpace个间隔需要再放一个空格*/
for (int k = 0; k < frontSpace; ++k)
{
tmp[k] += " ";
}
/*连接这些字符串*/
for (int k = 0; k < count; ++k)
{
str += tmp[k];
}//for
ret.push_back(str);
}//else
}//else
tmp.clear();
count = 0;
sumLen = 0;
--i;
}//for
/*处理最后一组,也就是尾行*/
/*只有一个单词,左对齐*/
if (1 == count)
{
string str = tmp[0];
while (str.length() < maxWidth)
str += " ";
ret.push_back(str);
}//if
if(count > 1){
string str = "";
/*末行的每个单词间放一个空格,其余空格放在尾部*/
for (int k = 0; k < count - 1; ++k)
{
str = str + tmp[k] + " ";
}//for
str += tmp[count - 1];
while (str.length() < maxWidth)
str += " ";
ret.push_back(str);
}//else
return ret;
}
};
LeetCode(68) Text Justification的更多相关文章
- LeetCode(68):文本左右对齐
Hard! 题目描述: 给定一个单词数组和一个长度 maxWidth,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本. 你应该使用“贪心算法”来放置给定的单词:也就是 ...
- LeetCode(68)-Compare Version Numbers
题目: Compare two version numbers version1 and version2. If version1 > version2 return 1, if versio ...
- Qt 学习之路 2(68):访问网络(4)
Home / Qt 学习之路 2 / Qt 学习之路 2(68):访问网络(4) Qt 学习之路 2(68):访问网络(4) 豆子 2013年11月7日 Qt 学习之路 2 19条评论 前面几章我们了 ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
随机推荐
- 对SSH三大框架的理解
SSH框架一般指的是Struts.Spring.Hibernate,后来Struts2代替了Struts.最近5年,Struts2已经被Spring MVC代替,而Hibernate基本也被iBati ...
- 【整理】更改MSSQL默认字符集
记录所有应用到 SQL Server 实例和当前排序规则的 Service Pack 和修补程序: SELECT SERVERPROPERTY('ProductVersion ') AS Produc ...
- [Linux]删除7天前的目录
find /ftp_data/ -mindepth 2 -type d -mtime +7 -exec rm -rf {} \;
- 高亮代码显示之HTML困惑
近期做样式库,需要将HTML代码高亮,开始寻找相关的插件. 看到highlight.js,看到它主题样式如此之多,支持语言也如此之多,以为找到了神器.不想这只是痛苦的开始,为了让它支持HTML,我尝试 ...
- 上传图片HTML <form> 标签的 method 属性
<!DOCTYPE HTML><html><body> <form action="/example/html5/demo_form.asp&quo ...
- 移动端自动化环境搭建-Appium for Windows的安装
安装Appium for Windows版 A.安装依赖 appium就是我们做移动端自动化测试主要的软件 B.安装过程
- Selenium+Python+Eclipse网页自动化集成环境配置(附简单的测试程序)
最近公司在给我们培训,主要是网页自动化测试的,现在的工作每天都是测APP,刚刚入门,不过,当我看了别人写的bug之后,就觉得不会觉得能够发现bug多么多么的厉害了. 前两周的时间一直在搭建自动化测试的 ...
- MySQL的备份的一些策略和方法的总结
1.一般使用的是mysqldump来进行备份,每次dump的数据是1000条,并且在这个过程中会进行锁表. (这种方式是逻辑备份,即直接将数据库中的数据导成sql语句进行备份的过程) 主要的使用方法: ...
- C#之Winform中treeview控件绑定数据库
private DataSet ds; private SqlDataAdapter sqlDataAdapter1; private int maxnodeid; private void Form ...
- 如何让我们的VMware虚拟机上网——转载
一.必须检查你的windows系统的VMware DHCP Service 和VMware NAT Service这个两个服务是否启动.如果未启动请启动. 1,在开始windows里面输入servic ...