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 ...
随机推荐
- 【javascript基础】4、原型与原型链
前言 荒废了好几天,在宿舍闷了几天了,一直想着回家放松,什么也没搞,论文就让老师催吧.不过,闲的没事干的感觉真是不好,还是看看书,写写博客吧,今天和大家说说函数的原型. 原型是什么 第一次看到这个的时 ...
- day6-2面向对象
概述: 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发“更快更好更强...” 注:Java和C#来 ...
- click事件
click事件是可以多次绑定的,如果绑定多次就会执行多次,因此再不需要重复执行的情况下,就需要使用unbind对事件进行解绑
- appium入门
前语:学习需要总结,或许有些知识自己存在偏差,但是能总结出来就会更加加深所学知识 1. 环境变量配置 必备软件安装: jdk1.6.0 android-sdk python:2.7(3.6 ...
- python笔记一
好奇,想一探究竟.安装就出点小问题,win7,64位,一直卡在这里不动了? 只好取消.第二天安装仍是如此. 于是下载Windows6.1-KB2999226-x64.msu,安装,仍卡顿不动: 于是找 ...
- git 代码组织
在20145306CSAPP2E文件夹下建立相应的文件夹: src:存放源代码文件 include: 存放头文件 bin:存放编译后的目标文件.可执行文件等 lib:存放项目所需的静态库.动态(共享) ...
- 使用虚幻引擎中的C++导论(二-UE4基类)
使用虚幻引擎中的C++导论(二) 第一,这篇是我翻译的虚幻4官网的新手编程教程,原文传送门,有的翻译不太好,但大体意思差不多,请支持我O(∩_∩)O谢谢. 第二,某些细节操作,这篇文章省略了,如果有不 ...
- Repeater控件 ---属性(ItemCommand事件)
epeater的Command操作:1.ItemCommand事件 - 在Repeater中所有能触发事件的控件,都会来触发这一个事件 2.CommandName - 判断点击的是什么按钮,e.Com ...
- ABAP-SQL基础知识
SQL语法 我们在编写ABAP4程序的时候,经常需要从TABLE中根据某些条件读取数据,读取数据最常用的方法就是通过SQL语法实现的.ABAP/4中可以利用SQL语法创建或读取TABLE,SQL语法分 ...
- [翻译]Shape comparison language
link: http://www.cnblogs.com/yhlx125/p/3635623.html Shape comparison language 首先说说我遇到的一个问题: IR ...