【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-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- Java中设置classpath、path、JAVA_HOME的作用
Thinking in Java对classpath作用的解释 Java 解释器的工作程序如下: 首先,它找到环境变量CLASSPATH(将Java 或者具有Java 解释能力的工具——如浏览器——安 ...
- JavaBean出现的目的
一.JavaBean出现的目的 由于只用JSP开发会发现使用了很多的Scriptlet,会使得jsp文件非常混乱.如果使用了JavaBean,则可以大大减少JSP中代码量. JSP的理想状态是只负责显 ...
- Unlink of file '.git/objects/pack/pack-***.pack' failed. Should I try again? (y/n) (转)
git pull的时候遇到 Unlink of file '.git/objects/pack/pack-***.pack' failed. Should I try again? (y/n) y 于 ...
- MongoDB设置访问权限、设置用户(转)
MongoDB已经使用很长一段时间了,基于MongoDB的数据存储也一直没有使用到权限访问(MongoDB默认设置为无权限访问限制),今天特地花了一点时间研究了一下,研究成果如下: 注:研究成果基于W ...
- Java8初体验(二)Stream语法详解
感谢同事[天锦]的投稿.投稿请联系 tengfei@ifeve.com 上篇文章Java8初体验(一)lambda表达式语法比 较详细的介绍了lambda表达式的方方面面,细心的读者会发现那篇文章的例 ...
- solr6.1-----相关配置-详细介绍-启动-全文检索
环境准备 jdk1.8.0_60 + apache-tomcat-8.5.4 + solr-6.1.0 进过测试.使用tomcat 7.x 不能正常启动solr .会报错,至于怎么原因,lz 暂时 ...
- 11个Visual Studio代码性能分析工具
软件开发中的性能优化对程序员来说是一个非常重要的问题.一个小问题可能成为一个大的系统的瓶颈.但是对于程序员来说,通过自身去优化代码是十分困难的.幸运的是,有一些非常棒的工具可以帮助程序员进行代码分析和 ...
- 关于高性能Web服务的一点思考
下面这些概念对于专业做性能测试的会比较熟悉,但是对于开发人员不一定都那么清楚. 并发用户数: 某一时刻同时请求服务器的用户总数,通常我们也称并发数,并发连接数等. 吞吐率:对于web服务器来说就是每秒 ...
- Ubuntu 14 编译安装 XDebug - 2.3.3 For PHP - 5.4.45
安装过程如下: 1.下载XDebug源码:http://xdebug.org/files/xdebug-2.3.3.tgz 2.解压到某个目录,如 /opt/software/xdebug-2.3.3 ...
- JS写的多级联select,如何取值
var $ = function (id) { return "string" == typeof id ? document.getElementById(id) : id ...