【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-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- C语言中的参数传递
有空看看: c语言 函数传输传递的三种方式(值.指针.引用) C语言之参数传递 C语言形参和实参,传值调用和引用调用的区别
- kafka环境搭建及librdkafka测试
kafka环境搭建及librdkafka测试 (2016-04-05 10:18:25) 一.kafka环境搭建(转自http://kafka.apache.org/documentation.h ...
- oracle vm virtualbox右ctrl切换显示模式
转自: http://blog.csdn.net/lyc_daniel/article/details/44195515 virtualbox里面有个HOME键,注意这个HOME键不一定是键盘上的HO ...
- Myeclipse如何关联源码
Myeclipse版本:Myeclipse2014 关联源码前要下载对应的源码,如本例的dom4j-1.6.1.jar,则去下载对应的源码dom4j-1.6.1.zip 如果做的是web项目,就要将该 ...
- MapReduce的MapTask任务的运行源码级分析
TaskTracker任务初始化及启动task源码级分析 这篇文章中分析了任务的启动,每个task都会使用一个进程占用一个JVM来执行,org.apache.hadoop.mapred.Child方法 ...
- solr多条件查询(二)
由于现在的 需求很变态需要N多条件的叠加,本人就用了一天时间摸索加求助,终于参透出这个q和fq的强大之处. 需求如下图,有三种关系:并且.或.不含 1.如果是或者也就是改变的q的查询条件: 2.如果是 ...
- web开发前端学习
bootstrap: http://www.bootcss.com/ bootstrap: http://bootsnipp.com/snippets/featured/single-colum ...
- [Kerberos] How to Kerberize an Hadoop Cluster
Overview Kerberos是一个第三方认证机制,用户和服务(known as principals)通过kerberos server (known as the Key Distributi ...
- ggplo2学习笔记——基本图形类型
1.散点图:又称散点分布图,是以一个变量为恨坐标,另一个变量为纵坐标,利用散点(坐标点)的分布形态反映变量统计关系的一种图形.可以用来确认两个变量之间的关系.绘制自由曲线.矩阵关联分析等. 2.条形图 ...
- boss设计参考的脑图