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. windows系统下的两个批处理命令

    启动应用:***.exe 关闭应用:taskkill /f /im ***.exe 保存为.bat文件

  2. Unity3d 判断物体是否在可见范围内

    unity中自带两个回调函数: void OnBecameVisible()//当物体可见时,回调一次. void OnBecameInvisible()//当物体不可见时,回调一次. 在untiy编 ...

  3. 【线段树 泰勒展开】Codechef April Challenge 2018 Chef at the Food Fair

    第一次写泰勒展开:本地和CC差距好大 题目大意 大厨住的城市里办了一场美食节.一条街上开设了$N$个摊位,编号为$1∼N$.这天开始时,第$i$个摊位的食物会导致食物中毒的概率是$P_i$.在这一天中 ...

  4. PHP redis使用命令

    很有用;以下是redis官方提供的命令使用技巧: 下载地址如下: https://github.com/owlient/phpredis(支持redis 2.0.4) Redis::__constru ...

  5. 使用selenium和phantomJS浏览器登陆豆瓣的小演示

    # 使用selenium和phantomJS浏览器登陆豆瓣的小演示 # 导入库 from selenium import webdriver # 实例化一个浏览器对象 web = webdriver. ...

  6. 多本Python极速入门最佳书籍,不可错过的Python学习资料!

    Python作为现在很热门的一门编程语言,介于Python的友好,许多的初学者都将其作为首选,为了帮助大家更好的学习Python,我筛选了2年内优秀的python书籍,个别经典的书籍扩展到5年内.   ...

  7. excel日期格式取年份

    具体思路:先将日期格式更改为常规格式,再取常规格式的前4位数字 例如:A1==1981/12/22 第一步B1=TEXT(A1,"emd") 第二步C1=LEFT(B1,4) 结束

  8. 光学字符识别OCR-6 光学识别

    经过前面的文字定位和文本切割,我们已经能够找出图像中单个文字的区域,接下来可以建立相应的模型对单字进行识别. 模型选择         在模型方面,我们选择了深度学习中的卷积神经网络模型,通过多层卷积 ...

  9. java中,为什么char类型数组可以直接用数组名打印,打印结果居然不是地址值!

    char类型的数组就相当于一个字符串. 因为输出流System.out是PrintStream对象,PrintStream有多个重载的println方法,其中一个就是public void print ...

  10. 大数据学习——scala函数与方法

    package com /** * Created by Administrator on 2019/4/8. */ object TestMap { def ttt(f: Int => Int ...