[LeetCode] Text Justification words显示的排序控制
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.
- 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.
- 字符串截取,比较方便的是实现代码已经截取好,不用我们截取了。
- 如果一个word 长度超过 一行的长度怎么处理,这个测试例子中没有考虑,所以没有实现这一部分。
- 一行中的左右端没有空格。
- 如果行中只有一个word ,空格添加在右边。
- 行中空格尽量平分,不够时多的在左侧。
- 最后一行words 之间只需要一个空格间隔,末尾需要补齐空格。
- 困难的地方是怎么算空格,一行有多少个word 好处理,word 之间多少空格就比较麻烦。
思路:
- 遍历输入words
- 计算已经输入 word长度加上空格1,和已经输入word 的长度,如果还没有超出约束,continue
- 判断已经输入 word 的个数,如果是1个,添加空格后输入。更新行标记、2中用到的记录变量。
- 如果word 为多个,则计算需要填多少个空格,需要填的位置的个数(word个数-1)。
- 填写该行的string,然后更新标记。
- 遍历结束后判断最后一行的情况。
计算多个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 ;
}
- for遍历输入的words
- for查找k=0,从上一级for下标开始,寻找放入一行的word 的个数,同时记录word 长度和,k 结束为不取值
- for j=0填写如何行
- 如果i + k>= n,表示这是末尾行,word 之间添加一个空格。
- 否则,通过 (L-len)/(k-1)+(j<( (L-len)%(k-1) )),为各位置的空格数
- 更新行末尾空格(最后一行的情况),将行添加到ret 中。
- 结束
#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显示的排序控制的更多相关文章
- [LeetCode] Text Justification 文本左右对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- [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 文本对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- leetcode@ [68] Text Justification (String Manipulation)
https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...
- 【一天一道LeetCode】#68. Text Justification
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- LeetCode OJ——Text Justification
http://oj.leetcode.com/problems/text-justification/ 编译代码要看warnings!它提供了可能出问题的情况,比如类型转换上unsigned int ...
- [LeetCode] 68. Text Justification 文本对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
随机推荐
- 03_5_static关键字
03_5_static关键字 1. static关键字 在类中,用static声明的成员变量为静态成员变量,它为该类的公用 变量,在第一次使用时被初始化,对于该类的所有对象来说,static成员变量只 ...
- 转 消息队列之 RabbitMQ
转 https://www.jianshu.com/p/79ca08116d57 消息队列之 RabbitMQ 预流 2017.05.06 16:03* 字数 4884 阅读 80990评论 18喜欢 ...
- 如何用纯 CSS 创作一副国际象棋
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/WyXrjz 可交互视频 ...
- json_encode() 避免转换中文
json_encode() 避免转换中文 我们都知道,json_encode()可以将数据转换为json格式,而且只针对utf8编码的数据有效,而且在转换中文的时候,将中文转换成不可读的”\u***” ...
- day 37 MySQL行(记录)的详细操作
MySQL行(记录)的详细操作 阅读目录 一 介绍 二 插入数据INSERT 三 更新数据UPDATE 四 删除数据DELETE 五 查询数据SELECT 六 权限管理 一 介绍 MySQL数据操 ...
- phpmyadmin提示The mbstring extension is missing的解决方法
解决办法:安装php-mbstring yum install php-mbstring
- HDU - 1251 统计难题(Trie树)
有很多单词(只有小写字母组成,不会有重复的单词出现) 要统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). 每个单词长度不会超过10. Trie树的模板题.这个题内存把控不好容易MLE. ...
- pandas修改列名
- LA 3667 Ruler 搜索
题意: 给出\(n\)个长度,要设计一个有\(m\)个刻度的刻度尺,刻度尺的刻度从\(0\)开始. 使得任意一个长度都能被该刻度尺度量出来. 首先要使\(m\)最小,在\(m\)最小的前提下尺子的长度 ...
- Linux下配置LAMP环境
先准备相关软件,并确保服务器已经安装了gcc,gcc-c++,make三个软件,以便后续编译过程. 首先安装, libxml2 ftp://xmlsoft.org/libxml2/ 下载最新版本(我的 ...