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 exactlyL 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.

click to show corner cases.

Corner Cases:

    • 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.
Hide Tags

String

 

 
   这题有点复杂,关于word显示的排序控制,直观的就是用word 写英文文档,自动调整一行中的空格长度,达到一行中的空格尽量平均。
 
需要考虑的情况:
  • 字符串截取,比较方便的是实现代码已经截取好,不用我们截取了。
  • 如果一个word 长度超过 一行的长度怎么处理,这个测试例子中没有考虑,所以没有实现这一部分。
  • 一行中的左右端没有空格。
  • 如果行中只有一个word ,空格添加在右边。
  • 行中空格尽量平分,不够时多的在左侧。
  • 最后一行words 之间只需要一个空格间隔,末尾需要补齐空格。
  • 困难的地方是怎么算空格,一行有多少个word 好处理,word 之间多少空格就比较麻烦。

思路:

  1. 遍历输入words
  2. 计算已经输入 word长度加上空格1,和已经输入word 的长度,如果还没有超出约束,continue
  3. 判断已经输入 word 的个数,如果是1个,添加空格后输入。更新行标记、2中用到的记录变量。
  4. 如果word 为多个,则计算需要填多少个空格,需要填的位置的个数(word个数-1)。
  5. 填写该行的string,然后更新标记。
  6. 遍历结束后判断最后一行的情况。

  计算多个word 时空格的情况,通过记录空格的个数spaceLeft,还有位置个数spaceAddr,(spaceLeft+spaceAddr-1)/spaceAddr,则为最左边的位置空格个数,然后更新 spaceLeft 和 spaceAddr,这样就可以在行添加word 时候把words 计算进去。

我写的代码:

 #include <string>
#include <vector>
#include <iostream>
using namespace std; class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L) {
int curLen = ,startIdx = ,curWordIdx =-,wordLen =;
int spaceLeft=,spaceAddr=,temp=;
vector<string> ret;
string line="";
while(++curWordIdx<words.size()){
if(curLen+words[curWordIdx].size()<=L){
curLen+=words[curWordIdx].size()+;
wordLen+= words[curWordIdx].size();
continue;
}
if(startIdx+==curWordIdx)
line=words[startIdx]+string(L-words[startIdx].size(),' ');
else{
spaceLeft=L - wordLen;
spaceAddr = curWordIdx - startIdx -;
line = words[startIdx];
// cout<<spaceLeft<<" "<<spaceAddr<<endl;
while(++startIdx<curWordIdx){
temp = (spaceLeft+spaceAddr-)/spaceAddr;
line+=string(temp,' ');
line+=words[startIdx];
spaceLeft-=temp;
spaceAddr--;
}
}
ret.push_back(line);
spaceLeft = spaceAddr =wordLen= curLen= ;
startIdx = curWordIdx;
curWordIdx --;
line="";
}
if(curLen>){ line = words[startIdx];
while(++startIdx<curWordIdx){
line+=" "+words[startIdx];
}
line+=string(L-line.size(),' ');
ret.push_back(line);
}
return ret;
}
}; int main()
{
vector<string> words={"What","must","be","shall","be."};
int l = ;
Solution sol;
vector<string> ret = sol.fullJustify(words,l);
for(int i =;i<ret.size();i++)
cout<<ret[i]<<"*"<<endl; return ;
}
 
discuss 中有一个很少行数的实现,大概思路是一样的,写的比我简略,同时处理空格情况比我写的好,过程如下:
 
  1. for遍历输入的words
  2.   for查找k=0,从上一级for下标开始,寻找放入一行的word 的个数,同时记录word 长度和,k 结束为不取值
  3.   for j=0填写如何行
      • 如果i + k>= n,表示这是末尾行,word 之间添加一个空格。
      • 否则,通过 (L-len)/(k-1)+(j<( (L-len)%(k-1) )),为各位置的空格数
  4. 更新行末尾空格(最后一行的情况),将行添加到ret 中。
  5. 结束
 
代码如下:
 
 #include <string>
#include <vector>
#include <iostream>
using namespace std;
/**
class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L) {
int curLen = 0,startIdx = 0,curWordIdx =-1,wordLen =0;
int spaceLeft=0,spaceAddr=0,temp=0;
vector<string> ret;
string line="";
while(++curWordIdx<words.size()){
if(curLen+words[curWordIdx].size()<=L){
curLen+=words[curWordIdx].size()+1;
wordLen+= words[curWordIdx].size();
continue;
}
if(startIdx+1==curWordIdx)
line=words[startIdx]+string(L-words[startIdx].size(),' ');
else{
spaceLeft=L - wordLen;
spaceAddr = curWordIdx - startIdx -1;
line = words[startIdx];
// cout<<spaceLeft<<" "<<spaceAddr<<endl;
while(++startIdx<curWordIdx){
temp = (spaceLeft+spaceAddr-1)/spaceAddr;
line+=string(temp,' ');
line+=words[startIdx];
spaceLeft-=temp;
spaceAddr--;
}
}
ret.push_back(line);
spaceLeft = spaceAddr =wordLen= curLen= 0;
startIdx = curWordIdx;
curWordIdx --;
line="";
}
if(curLen>0){ line = words[startIdx];
while(++startIdx<curWordIdx){
line+=" "+words[startIdx];
}
line+=string(L-line.size(),' ');
ret.push_back(line);
}
return ret;
}
};
*/
class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L) {
vector<string> ret;
for(int i=,k,len;i<words.size();i+=k){
for( k=len=;i+k<words.size()&&len+words[i+k].size()+k<=L;k++)
len+=words[i+k].size();
string temp = words[i];
for(int j=;j<k-;j++){
if(i+k>=words.size()) temp+=" ";//for the last line.
else temp+=string((L-len)/(k-)+(j<( (L-len)%(k-) )),' ' );
temp+=words[i+j+];
}
temp+=string(L-temp.size(),' ');
ret.push_back(temp);
}
return ret;
}
}; int main()
{
vector<string> words={"What","must","be","shall","be."};
int l = ;
Solution sol;
vector<string> ret = sol.fullJustify(words,l);
for(int i =;i<ret.size();i++)
cout<<ret[i]<<"*"<<endl; return ;
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

[LeetCode] Text Justification words显示的排序控制的更多相关文章

  1. [LeetCode] Text Justification 文本左右对齐

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  2. [leetcode]Text Justification @ Python

    原题地址:https://oj.leetcode.com/problems/text-justification/ 题意: Given an array of words and a length L ...

  3. LeetCode:Text Justification

    题目链接 Given an array of words and a length L, format the text such that each line has exactly L chara ...

  4. LeetCode: Text Justification 解题报告

    Text Justification Given an array of words and a length L, format the text such that each line has e ...

  5. [Leetcode] text justification 文本对齐

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  6. leetcode@ [68] Text Justification (String Manipulation)

    https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...

  7. 【一天一道LeetCode】#68. Text Justification

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  8. LeetCode OJ——Text Justification

    http://oj.leetcode.com/problems/text-justification/ 编译代码要看warnings!它提供了可能出问题的情况,比如类型转换上unsigned int ...

  9. [LeetCode] 68. Text Justification 文本对齐

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

随机推荐

  1. 51nod——2504 是子序列的个数(一看就会的序列自动机原理)

    还以为序列自动机是什么,写完无意间看到帖子原来这就是序列自动机……这算自己发现算法

  2. CentOS 编译安装PHP5.6(7以上也通用)

    由于公司有新服务器需要构建一套LNMP平台,且需要编译安装各个部件,所以记录下此文章. 这是安装PHP涉及到的软件包(可以自行决定使用哪个版本): ├── libiconv-1.15.tar.gz ├ ...

  3. BZOJ 2002 弹飞绵羊(分块)

    题目:弹飞绵羊 这道题,据说是lct裸题,但是lct那么高级的数据结构,我并不会,所以采取了学长讲过的分块做法,我们对序列分块,可以定义两个数组,其中一个表示从当前位置跳出当前块需要多少步,另一个数组 ...

  4. 201621123080《Java程序设计》第9周学习总结

    作业09-集合与泛型 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集集合 1. List中指定元素的删除(题集题目) 1.1 实 ...

  5. Altium Designer入门学习笔记1.软件安装与资料收集

    一.软件安装 微信:http://url.cn/5Eudzt9 关注微信公众号"软件安装管家",点击"软件目录",弹出"软件目录",点击进入 ...

  6. Java策略模式(Strategy)

    一.定义 定义一组算法,将每个算法都封装起来,并且使它们之间可以互换.策略模式使这些算法在客户端调用它们的时候能够互不影响地变化.(Java的TreeSet集合中,构造方法可传入具体的比较器对象以实现 ...

  7. adb -a server nodaemon,设备一直显示 offline,而 adb devices 一直显示 device【已解决】

    1. adb -a server nodaemon 一直显示 offline 2. adb devices 一直显示 device 谷歌 和 度娘了一圈,未寻得解决办法 # 解决方法 问题已解决,使用 ...

  8. tomcat6-输入输出buffer设计

    之前写的一个ppt 搬到博客来

  9. python学习-- class 类中需要注意的地方

    from django.db import models class Person(models.Model):     name = models.CharField(max_length=30) ...

  10. xml ,html,xhtml

    html,xhtml和xml的定义: 1.html即是超文本标记语言(Hyper Text Markup Language),是最早写网页的语言,但是由于时间早,规范不是很好,大小写混写且编码不规范: ...