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. Eclipse+Tomcat搭建jsp服务器

    首先,安装java sdk 环境,这里就不多说了,附上java sdk的下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk ...

  2. 三倍经验——bzoj3663、4660、4206 Crazy Rabbit/最大团

    题目描述: 3663 4660 4206 题解: 第一眼:不成立的互相连边,然后用网络流求解无向图最小点覆盖! 好吧我不会. 正解: 每个点对应圆上的一段圆弧,长这样: 设对应圆弧$(l,r)$. 若 ...

  3. apicloud入门学习笔记1:简单介绍

    官网地址:https://www.apicloud.com/ 新手开发指南:https://docs.apicloud.com/APICloud/junior-develop-guide 开发语言:H ...

  4. German Collegiate Programming Contest 2015

    // Legacy Code #include <iostream> #include <cstdio> #include <cstring> #include & ...

  5. Markdown 使用锚点

    首先是建立一个跳转的连接: [说明文字](#jump) 然后标记要跳转到什么位置即可: <span id = "jump">跳转到这里:</span>

  6. CodeForces 489F DP Special Matrices

    首先统计一下前m行中,有x列1的个数为0,有y列1的个数为1. 设d(i, j)表示有i列1的个数为0,有j列1的个数为1,能到达这个状态的矩阵的个数. 则d(x, y) = 1 每一行都是两个1一起 ...

  7. 更改activity切换方式

    overridePendingTransition(enterAnim, exitAnim); Intent intent =new Intent(this,item2.class); startAc ...

  8. MMM的一周计划 准备公告

    (19.6.17——19.6.22) 目前本周还没有过去所以还会更新 第0周 目前博客更新暂定于 [题目难度颜色见洛谷] 1.绿题以上绝对更新 2.黄题可能更新 3.其他估计不会有更新 准备工作 1. ...

  9. Go语言学习03

    Go语言-数组类型 一个数组(Array)就是一个可以容纳若干类型相同的元素的容器.这个容器的大小(即数组的长度)是固定的,且是体现在数组的类型字面量之中的.比如,我们声明了一个数组类型: type ...

  10. [python IO学习篇] [打开包含中文路径的文件]

    https://my.oschina.net/mcyang000/blog/289460   打开路径含有中文的文件时,要注意: 1 在windows对文件名编码是采用gbk等进行编码保存,所以要将文 ...