【leetcode】Text Justification
Text Justification
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.
1.每两个单词之间至少一个空格
class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L) { int n=words.size(); vector<string> result; int count=;
int totalWordsLen=;
int startIndex=; string tmpStr; for(int i=;i<n-;i++)
{
count++;
totalWordsLen+=words[i].size();
int len=totalWordsLen+count-; if(len+words[i+].size()+>L)
{
tmpStr=formLine(startIndex,i,L,totalWordsLen,words);
result.push_back(tmpStr); count=;
totalWordsLen=;
startIndex=i+;
}
} tmpStr=formLastLine(startIndex,L,words);
result.push_back(tmpStr); return result;
}
string formLine(int start,int end,int &L,int totalWordsLen,vector<string> &words)
{
int n=end-start;
int spaceNum=L-totalWordsLen;
if(n==) return words[start]+string(spaceNum,' '); int n1=spaceNum/n;
int n2=spaceNum%n; vector<int> space(n,n1);
for(int i=;i<n2;i++) space[i]+=; string tmpStr="";
for(int i=start;i<end;i++)
{
tmpStr+=(words[i]+string(space[i-start],' '));
} tmpStr+=words[end];
return tmpStr; } string formLastLine(int start,int &L,vector<string> &words)
{
string tmpStr=words[start];
for(int i=start+;i<words.size();i++)
{
tmpStr+=' '+words[i];
}
while(tmpStr.length()<L)
{
tmpStr+=' ';
}
return tmpStr;
}
};
【leetcode】Text Justification的更多相关文章
- 【leetcode】Text Justification(hard) ☆
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- leetcode@ [68] Text Justification (String Manipulation)
https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- rpm 命令
这些事rpm的常用参数!!! 你可以在linux下man 一下rpm就知道了!!! 不过是英文的,不然你可以百度一下rpm就知道了额!!! 下面我帮你贴几个!!!!rpm 常用命令1.安装一个包 # ...
- PHP使用数据库的并发问题(转)
在并行系统中并发问题永远不可忽视.尽管PHP语言原生没有提供多线程机制,那并不意味着所有的操作都是线程安全的.尤其是在操作诸如订单.支付等业务系统中,更需要注意操作数据库的并发问题. 接下来我通过一个 ...
- 单选框的回显c:if
<input type="radio" name="sex" value="boy" <c:if test="${te ...
- PHPstorm设置连接FTP,进行文件上传、下载、比较
内容转载自:http://www.cnblogs.com/jikey/p/3486621.html 如何在 ...
- [IOC]Unity使用
Unity是什么? unity是patterns&practices团队开发的一个轻量级.可扩展的依赖注入容器. Unity特性 1.它提供了创建(或者装配)对象实例的机制,而这些对象实例可能 ...
- javascript数组的知识点讲解
javascript数组的知识点讲解 阅读目录 数组的基本方法如下 concat() join() pop() push() reverse() shift() sort() splice() toS ...
- 大熊君大话NodeJS之开篇------Why NodeJS(将Javascript进行到底)
一,开篇分析 大家好啊,大熊君又来啦(*^__^*) 嘻嘻……,之前我写过一系列关于JS(OOP与设计模式)方面的文章,反响还好,其实这也是对我本人最大的鼓励,于是我决定我要将JavaScript进行 ...
- 【AngularJS】—— 1 初识AngularJs
怀着激动与忐忑的心情,开始了学习AngularJS的旅程,很久之前就听说了这个前端框架,但是由于自己一直没有从事相关的工作,因此也没有进行学习.这次正好学习AngularJS,直接复习一下前端的知识. ...
- POJ 1905 Expanding Rods
Expanding Rods Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1 ...
- HNU 12886 Cracking the Safe(暴力枚举)
题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...