[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 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. "
]
Note: Each word is guaranteed not to exceed L in length.
- 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.
题意:
1)给定单词数组和指定长度L,以左右对齐的形式,排列单词使每行字符个数为L;
2)每行尽可能的多放单词,剩余的部分放“ ”以确保每行字符的个数;
3)单词之间的空格,尽量均匀分布,若没法均匀分布,左边的单词之间均匀的放多余空格;
4)最后一行,左靠齐,单词之间仅放一个空格,不足的右补齐。
思路:从词组里面取出一个单词,计算其长度,小于L,则,再取出一个词,考虑整个长度是否小于L,若是,则继续加词。其中要注意的是,每个单词之间至少有一个空格,所以在计算时要考虑空格的个数,显然,空格数等于目前单词的个数减1。这个过程的小技巧是,先计算当前行的字符串长度和和下一个单词的总长度是否小于L,在确定是否加上下一个单词,具体见代码。这样每一行的单词个数确定好了,这样就遇到一个问题,就是,每个单词之间的空格数不确定。在此之前,我们还要考虑如何插入空格。常规的思路是:使用中间变量(用数组)先将每个单词存好,然后等到确定好空格之间的个数后,再连接起来。现在就再次转到如何处理空格个数的问题了:
若该行只有一个单词,这个好办,剩下的直接补上“ ”就行;若有多个了?我们先用总的空格数除以空格个数,求得,每个单词之间只要有几个空格,然后再求余,求得多余的,再按从左往右的方式靠左的单词之间多插入一个“ ”。这样以后,又碰到一个问题,就是如何将其连接起来?常规思路是:先遍历整个数组中的string,将每个单词后面插入均等的个数的空格,然后在遍历左边几个单词加上余下的空格。
这样还是会遇到一个问题,那就是最后一行的处理方式和别的行不一样,这样,就可以在考虑某一个数的时候,考虑单词是否越界的问题,以保存,最后一行,单独处理。
这样写的过程中还是出错了,参考了小小程序媛,自己写 ,一次通过的还是很难啊!!
class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L)
{
if(words.empty()) return vector<string>();
vector<string> ret;
int sumLen=words.size();
vector<string> temp; //临时向量,只放字符串
int curLen=,count=; //该行的长度,和该行字符串的个数
for(int i=;i<sumLen;++i)
{ //注意if的条件使用
if((curLen+words[i].size()+count)<=L)
{
++count;
curLen+=words[i].size();
temp.push_back(words[i]);
continue;
}
else
{ //仅一个,剩下以空格补上
if(count==)
{
string str=temp[];
while(str.size()<L)
{
str+=" ";
}
ret.push_back(str);
}
else
{
string str;
//该行总的空格,
int extraSpace=L-curLen;
int everySpace=extraSpace/(count-); //每个间隔的空格
int frontSpace=extraSpace%(count-); //还需重新增加的空格
for(int k=;k<count-;++k)
{
int j=;
while(j<everySpace) //在插好的字符串后面接上空格
{
temp[k]+=" ";
++j;
}
}
for(int k=;k<frontSpace;++k) //重新接上还需增加的空格
{
temp[k]+=" ";
}
for(int k=;k<count;++k) //转换成字符串
{
str+=temp[k];
}
ret.push_back(str); //输出
}
}
//清零,准备下一行
temp.clear();
count=;
curLen=;
--i; //for循环中,i是先++后在判断是否满足条件,所以要— —
}
//处理最后一行,左对齐,一下执行的条件是,在for循环中if中的continue中断
if(count==)
{
string str=temp[];
while(str.size()<L)
{
str+=" ";
}
ret.push_back(str);
}
if(count>)
{
string str;
for(int k=;k<count-;++k)
{
str+=temp[k]+" ";
}
str+=temp[count-];
while(str.size()<L)
str+=" ";
ret.push_back(str);
}
return ret;
}
};
好吧,写完之后,代码比较长,上网拜读了Grandyang的写法,他(她)的写法,直接省去了中间数组,在将单词的连接和空格的处理在一起考虑了,所以代码比我的简洁很多。其大致思路为:找到每行所需的单词数,通过下标 j 是否等于words.size(),判断是否到了最后一行,分为两种情况考虑,一、最后一行,二、不是最后一行。通过确定每个单词的之后的空格数来实现,单词的连接和空格的插入同时进行。特别是针对,不是最后一行的情况,处理的比较巧妙,多个单词的情况,若是能,均分,everySpace就是均值,不能,则先给左边的空格多加1(体会这个),减去这个串空格,剩下的不会影响均分的值。针对多个时,遍历到最后一个时everySpace=space=0。感觉好绕,哈哈,懂了就是懂了,但还是不好想啊,这种处理方式,牛。多想想。这里直接给出其代码,并针对性的分析语句,若还是有不懂的,移步去看原文。
class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L)
{
vector<string> res;
int i=;
while(i<words.size())
{
int j=i; //记录每行起点的下标
int rowLen=;
while(j<words.size() && rowLen+words[j].size()+j-i<=L)
rowLen+=words[j++].size();
int space=L-rowLen; //每行中空格总数
string rowStr;
for(int k=i;k<j;++k)
{
rowStr+=words[k]; //space==0说明一行中仅有一个单词,且长度为L
if(space>)
{
int everySpace; //每个单词之间的间隔
if(j==words.size()) //最后一行的情况
{
if(j-k==) //只有一个单词剩下全为' '
everySpace=space;
else //多个单词,每个之间插入一个' '
everySpace=;
}
else
{
if(j-k->) //多个单词的情况下,每个之间有一个的情况下判断剩下多余的空格的放法,好好体会这个if
{
if(space%(j-k-)==) //均分
everySpace=space/(j-k-);
else
{
everySpace=space/(j-k-)+; //左边的多放1个' '
}
}
else //剩下一个单词
everySpace=space;
}
rowStr.append(everySpace,' ');
space-=everySpace;
}
}//end for
res.push_back(rowStr);
i=j;
}
return res;
}
};
[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] 68. Text Justification 文本对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- Text Justification,文本对齐
问题描述:把一个集合的单词按照每行L个字符放,每行要两端对齐,如果空格不能均匀分布在所有间隔中,那么左边的空格要多于右边的空格,最后一行靠左对齐. words: ["This", ...
- 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]Text Justification @ Python
原题地址:https://oj.leetcode.com/problems/text-justification/ 题意: Given an array of words and a length L ...
- LeetCode:Text Justification
题目链接 Given an array of words and a length L, format the text such that each line has exactly L chara ...
- 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 words显示的排序控制
Given an array of words and a length L, format the text such that each line has exactly L characters ...
随机推荐
- php Laravel5.5 表单验证常用的验证规则,以及示例
namespace App\Http\Controllers; use App\Models\Users; use Illuminate\Support\Facades\Validator; use ...
- 日志框架Log4j
log4j是一个用Java编写的可靠,快速和灵活的日志框架(API),它在Apache软件许可下发布.Log4j已经被移植到了C,C++,C#,Perl,Python和Ruby等语言中. Log4j是 ...
- python3 练习题100例 (十一)
题目十一:举例证明角谷猜想:以一个正整数N为例,如果N为偶数,就将它变为N/2,如果除后变为奇数,则将它乘3加1(即3N+1).不断重复这样的运算,经过有限步后,一定可以得到1. #!/usr/bin ...
- Python学习手册之函数和模块
在上一篇文章中,我们介绍了 Python 的控制结构,现在我们介绍 Python 函数和模块. 查看上一篇文章请点击:https://www.cnblogs.com/dustman/p/9976234 ...
- Java学习笔记六:Java的流程控制语句之if语句
Java的流程控制语句之if语句 一:Java条件语句之if: 我们经常需要先做判断,然后才决定是否要做某件事情.例如,如果考试成绩大于 90 分,则奖励一朵小红花 .对于这种“需要先判断条件,条件满 ...
- 二维数组快速排序(sort+qsort)
二维数组快速排序 qsort是c中快速排序,如果简单的一维数组排序,想必大家的懂.现在看一下二维数组的排序,虽然可以冒泡但是太费时间了,我们这里使用qsort来快速排序,看代码应该看得懂吧. 代码: ...
- go学习笔记-语言基础
语言基础 结构 基础组成: 包声明 引入包 函数 变量 语句 & 表达式 注释 程序 在开始编写应用之前,我们先从最基本的程序开始,在学习大部分语言之前,都会编写一个可以输出hello wor ...
- 动态规划(DP)算法
参考https://blog.csdn.net/libosbo/article/details/80038549 动态规划是求解决策过程最优化的数学方法.利用各个阶段之间的关系,逐个求解,最终求得全局 ...
- 最短路径算法 4.SPFA算法(1)
今天所说的就是常用的解决最短路径问题最后一个算法,这个算法同样是求连通图中单源点到其他结点的最短路径,功能和Bellman-Ford算法大致相同,可以求有负权的边的图,但不能出现负回路.但是SPFA算 ...
- LOOP AT SCREEN
用法 主に.画面の属性を変更させるために使用する. 照会モードでは入力不可とするが入力可能モードでは入力可能とする.ラジオボタンAが選択された場合はラジオボタンBに関連する項目は非表示とするなど. ...