【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-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- PHP isset 函数作用
isset函数是检测变量是否设置. 格式:bool isset ( mixed var [, mixed var [, ...]] ) 返回值: 若变量不存在则返回 FALSE 若变量存在且其值为NU ...
- Java使用Jetty实现嵌入式Web服务器及Servlet容器
Jetty是一个Java实现的开源的servlet容器,它既可以像Tomcat一样作为一个完整的Web服务器和Servlet容器,同时也可以嵌入在Java应用程序中,在Java程序中调用Jetty. ...
- 理解JavaScript中的arguments,callee,caller,apply
arguments 该对象代表正在执行的函数和调用它的函数的参数. [function.]arguments[n] 参数function :选项.当前正在执行的 Function 对象的名字. n : ...
- DWZ框架中ajax提交文件表单的处理(关闭当前dialog + 刷新父级navTab)
先重点关注两个js文件:dwz.ajax.js和dwz.core.js 流程: 1.回调iframeCallback <form xx enctype="multipart/form- ...
- linux下如何查看chm文件
转自:http://www.cnblogs.com/jesseZh/p/4036811.html (64位) sudo dpkg -i chmsee_1.3.0-2ubuntu2_amd64.d ...
- JS禁止WEB页面鼠标事件大全
<!--禁止鼠标右键代码-->:<noscript><ifra:<scriptlanguage=javas:<!--:if(window.Event):doc ...
- 【Junit】JUnit-4.12使用报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing错误
下载了最新的JUnit版本,是4.12,结果尝试使用发现总是报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing这样的错误, 上网查 ...
- hdu1757 A Simple Math Problem
Problem Description Lele now is thinking about a simple function f(x).If x < 10 f(x) = x.If x > ...
- Ruby学习之module
我们可以认为module是一个专门存放一系列方法和常量的工具箱. module和class非常像, 只是module不能创建实例也不能有子类, 它们仅仅能存放东西. 例如: module Circle ...
- Code First04---关于上下文DbContext
这章主要讲怎么配置DbContext的子类访问的数据库的位置. 我相信大家最经常使用的数据库位置的配置方式就是配置文件了,也就是通过App.Config 或Web.Config来配置要访问的数据库. ...