[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 ...
随机推荐
- mac 使用homebrew 安装mysql
1. 安装homebrew ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)" brew update ...
- [LUOGU] 4149 [IOI2011]Race
点分治裸题 #include<iostream> #include<cstring> #include<cstdio> using namespace std; i ...
- Ubuntu下搭建多用户多权限ftp
#1.切换root用户模式 (已在root模式下的请忽略) sudo -i #然后提示你输入当前用户密码,输入密码后回车后如果密码正确控制台就变成root@***:/# 了. #2.更新软件源 apt ...
- 使用jmeter做简单的压测(检查点、负载设置、聚合报告)
1.添加断言(检查点) 在需要压测的接口下添加--断言--响应断言,取接口响应中包含有的数据即可 检查点HTTP请求-->断言-->响应断言1.名称.注释2.Apply to//作用于哪里 ...
- Vue表单输入绑定
<h3>基础用法</h3> <p>你可以用<strong>v-model</strong>指令在表单input,textarea以及sele ...
- 收集的免费API接口
1.IP地址调用接口 这是淘宝的IP调用API http://ip.taobao.com/service/getIpInfo.php?ip=$ip 返回值:{"code":0,&q ...
- 科学计算库Numpy——numpy.ndarray
创建ndarray 元素类型 对于ndarray结构来说,里面所有的元素必须是同一类型的,如果不是的话,会自动的向下进行转换. 元素类型所占字节数 数组维数 元素个数 数组的维度 数组中填充固定值 索 ...
- 使用Github第一节
学习Github 1.目的: 借助github托管代码 2.基本概念(1): 仓库(Repository) 仓库用来存放项目代码,每个项目对应一个仓库,多个项目则对应多个仓库 收藏(Start) 收藏 ...
- aoj-0118 property distribution(搜索)
Time limit1000 ms Memory limit131072 kB タナカ氏が HW アールの果樹園を残して亡くなりました.果樹園は東西南北方向に H × Wの区画に分けられ.区画ごとにリ ...
- 重新造轮子之静态链接1(Static linking)
最近学习计算机病毒学的过程中,又讲到了静态链接的问题,联想到了之前保健哥在信息安全的课堂上向我们展示了一个没有main()函数的C程序到底应该如何编写.个人觉得这个小实验对于加深静态链接的过程的理解也 ...