[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 ...
随机推荐
- Symfony FOSUserBundle用户登录验证
symfony是一个由组件构成的框架,登录验证的也是由一些组件构成,下面就介绍一下FOSUserBundle的使用. 以symfony 3.3为例, 首先我们需要先安装一下FOSUserBundle. ...
- Hadoop参数调优
转自:http://blog.sina.com.cn/s/blog_6a67b5c50100vop9.html dfs.block.size 决定HDFS文件block数量的多少(文件个数),它会间接 ...
- Python的matplotlib模块的使用-Github仓库
import matplotlib.pyplot as plt import numpy as np import requests url='https://api.github.com/searc ...
- ruby 操作csv
1.读取csv 文件中读取:一次读入全部(设置headers使 CSV#shift() 以CSV::Row对象返回而不是数组:使 CSV#read() 返回 CSV::Table 对象而不是数 ...
- ctf题目writeup(9)
继续刷题,找到一个 什么 蓝鲸安全的ctf平台 地址:http://whalectf.xin/challenges (话说这些ctf平台长得好像) 1. 放到converter试一下: 在用十六进制转 ...
- matlab-罗曼诺夫斯基准则剔除粗大值
罗曼诺夫斯基准则原理 罗曼诺夫斯基准则又称 t检验准则,其特点是首先删除一个可疑的的测得值,然后按 t分布检验被剔除的测量值是否含有粗大误差 罗曼诺夫斯基准则 1)选取合适的显著度a,选择合适的数 ...
- 什么是Session共享?请举出使用场景
是指在一个浏览器对应多个Web服务时,服务端的Session数据需要共享.例如单点登录.Web服务器集群等场景都需要用到.多子服务. Session共享有多种解决方案,例如Tomcat插件,我最喜欢的 ...
- 获取.jar文件运行时所处的路径
在Windows控制台中运行.jar文件时的两种环境: (1)控制台当前所在目录是.jar文件所在的目录 (2)控制台当前所在目录不是.jar文件所在的目录 我的期望: 我希望在上述两种环境下均可以得 ...
- mybatis if标签比较字符串
项目中需要在mybatis后台比较字符串 因为mybatis映射文件使用的是ognl表达式,所以不能使用 <if test="type == '0'"> 解决: < ...
- python 正则表达式 (重点) re模块
京东的注册页面,打开页面我们就看到这些要求输入个人信息的提示.假如我们随意的在手机号码这一栏输入一个11111111111,它会提示我们格式有误.这个功能是怎么实现的呢?假如现在你用python写一段 ...