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. Java String Integer转换 练习:编程求字符串“100”和“150”按十进制数值做差后的结果以字符串形式输出。

    package com.swift; public class String_To_Integer_Test { public static void main(String[] args) { /* ...

  2. Kenneth A.Lambert著的数据结构(用python语言描述)的第一章课后编程答案

    第6题:工资部门将每个支付周期的雇员信息的列表保存到一个文本文件, 每一行的格式:<last name><hourly wage><hours worked> 编写 ...

  3. 【贪心 计数】bzoj2006: [NOI2010]超级钢琴

    这么经典的贪心我怎么现在才做啊…… Description 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的 音乐. 这架超级钢琴可以弹奏出n个 ...

  4. tempfs详解

    致因 在平常工作中,我们经常需要查看Linux服务器磁盘挂载使用情况,可以使用df命令,不知大家注意到没有,我们使用此命令除了会查看到系统盘以及数据盘挂载情况,还会看到一个tmpfs也在挂载. [ro ...

  5. ASP( VBScript ) 解析 JSON

    <script language="jscript" runat="server"> Array.prototype.get = function( ...

  6. 详解wordpress如何把文件保存到阿里云OSS上!

    自己搞了一个Wordpress的博客,装完之后一直晾着没管,最近闲来开荒.为了减小服务器的带宽.存储.CUP的压力,决定把博客中的所有文件都保存到阿里云OSS上面. 关于这个问题,自己去调用OSS的S ...

  7. Python PyAudio 安装使用

    Python PyAudio安装: Python3.7 无法安装pyaudio pip install pyaudio提示error: Microsoft Visual C++ 14.0 is req ...

  8. Python使用ORM控制MongoDB(MongoEngine)

    简介: MongoEngine是一个对象文档映射器(ODM),相当于一个基于SQL的对象关系映射器(ORM) pymongo来操作MongoDB数据库,但是直接把对于数据库的操作代码都写在脚本中,这会 ...

  9. 使用IAR在开发nordic问题记录

    使用IAR在开发nordic的sdk的时候,官方有一段话*****Note for IAR 8 users:(Libraries for IAR 8 require wchar_t to be of ...

  10. Memcached配置失误引发的Keystone token丢失的问题

    故障现象 近期公司的OpenStack上频繁出现虚拟机创建失败的情况,查看日志定位到问题出在neutron-server向keystone认证token失败. 故障原因 Keystone所使用的Mem ...