[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 ...
随机推荐
- Java : 实体类不能序列化异常
当修改实体类之后调用接口出现不能序列化的异常时,一定要检查实体之间的关系是否都是正确的. could not serialize; nested exception is org.hibernate. ...
- JS 定时器,定时调用PHP
$(function() { var voiceplay=function(){ var site = location.href.split('_cms')[0] + '_cms/'; $.ajax ...
- 20145202 《Java程序设计》实验五实验报告
一.实验内容 1.用书上的TCP代码,实现服务器与客户端. 2.客户端与服务器连接 3.客户端中输入明文,利用DES算法加密,DES的秘钥用RSA公钥密码中服务器的公钥加密,计算明文的Hash函数值, ...
- 【转】Git远程操作详解
Git是目前最流行的版本管理系统,学会Git几乎成了开发者的必备技能. Git有很多优势,其中之一就是远程操作非常简便.本文详细介绍5个Git命令,它们的概念和用法,理解了这些内容,你就会完全掌握Gi ...
- P1103 书本整理
P1103 书本整理 题目描述 Frank是一个非常喜爱整洁的人.他有一大堆书和一个书架,想要把书放在书架上.书架可以放下所有的书,所以Frank首先将书按高度顺序排列在书架上.但是Frank发现,由 ...
- 如何删除TFS项目
TFS是先建集合,再在集合下面建项目.删除的时候,需要先删除项目,再删除集合,然后重新建.具体步骤如下: 1.删除项目 删除项目必须通过命令来进行删除,调用TFSDeleteProjec ...
- Java开发WebService(使用Java-WS)
前言: 初学Java,因为工作需要,直接跳到开发WebService.以前用.NET开发过WebService,对比一下,Java的WebService开发部署难度高了不止一个档次.网上的教程各式各异 ...
- 【vim环境配置】解决ubuntu上 由YouCompleteMe插件配置不当引起的 自动补全失效的问题
背景: 由于不可抗拒的原因,学习环境由之前centos的一台机器上,变成了ubuntu的一台机器上.因此,需要在新的ubuntu的机器上再配置一次vim环境.算起来这已经是第三次配置vim环境了(ma ...
- safari 移动下开启 滚定回弹
-webkit-overflow-scrolling : touch;
- 「日常训练」「小专题·图论」Domino Effect(1-5)
题意 分析 这题几乎就是一条dijkstra的问题.但是,如何考虑倒在中间? 要意识到这题求什么:单源最短路的最大值.那么有没有更大的?倒在中间有可能会使它更大. 但是要注意一个问题:不要把不存在的边 ...