LeetCode_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 exactly L 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.
分析: 主要注意两个地方。一, 正常的句子单词之间的空格数目是一;二,最后一行句子按正常处理
class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<string> res;
if(words.size() < ) return res; int start , end, len , evenSpace, bonus; end = ;
while(true){ len = ;
for(start = end; end < words.size(); ++end){
if(len +(end - start) + words[end].length() > L) break;// normal sentence has one space between words
len += words[end].length();
}
end--; if(end == start){ // only one word
string line = words[end];
line.append(L - len, ' ');
res.push_back(line);
}else{ // multiply words evenSpace = (L - len)/(end - start);
bonus = L -len - evenSpace*(end - start);
bool isLastLine = (end == words.size() -);
if(isLastLine){
evenSpace = ;
bonus = ;
} string line = words[start];
for(int i = start + ; i <= end; ++i)
{
int space = evenSpace;
if(bonus > )
{
--bonus;
++space;
}
line.append(space,' ');
line.append(words[i]);
} if(isLastLine){
line.append(L-len - (end - start), ' ');
} res.push_back(line);
} end++;
if(end == words.size()) break;
} return res;
}
};
LeetCode_Text Justification的更多相关文章
- [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
Text Justification Given an array of words and a length L, format the text such that each line has e ...
- 【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(68) Text Justification
题目 Given an array of words and a length L, format the text such that each line has exactly L charact ...
- LeetCode:Text Justification
题目链接 Given an array of words and a length L, format the text such that each line has exactly L chara ...
- Java for LeetCode 068 Text Justification
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- 68. Text Justification
题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...
- leetcode@ [68] Text Justification (String Manipulation)
https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...
- juce Justification 分析
很简单的一个类,一个rect放置在另一个rect中如何放置.只是没有考虑边距等,估且认为是在外层作考虑吧.然后认为是外框比内框大,所以外层怕是要进行检查才行 #ifndef JUCE_JUSTIFIC ...
随机推荐
- gis论坛
http://bbs.csdn.net/forums/GIS/ http://forums.mysql.com/list.php?23 http://www.remotegis.net/ http:/ ...
- 配置Delphi工具菜单 转
配置Delphi工具菜单 Delphi工具菜单是可配置的.缺省时,Delphi Tools工具菜单的菜单项为[Database Desktop].[Image Editor].[Package Col ...
- EF 5.0 帮助类 增删改查
原文地址:http://www.cnblogs.com/luomingui/p/3362813.html EF 5.0 帮助类 加入命名空间: using System; using System.D ...
- Oracle 表分析
ANALYZE TABLE SeikyuTbl COMPUTE Statistics FOR TABLE FOR ALL COLUMNS FOR ALL INDEXES ; 一.优化器的优化方式 Or ...
- iOS--九宫格布局
[self rankWithTotalColumns: andWithAppW: andWithAppH:]; //九宫格布局 - (void)rankWithTotalColumns:(int)to ...
- springBoot学习
http://blog.csdn.net/xiaoyu411502/article/details/47864969 博客: http://blog.csdn.net/xiaoyu411502/art ...
- Android软键盘强制弹出,隐藏输入法.
本文实例讲述了Android实现弹出键盘代码,是一个非常实用的功能.代码非常简洁.分享给大家供大家参考. 具体功能代码如下: ? 1 2 3 4 5 6 7 8 Timer timer = new T ...
- Loadrunner结果分析Graphs
Transactions(用户事务分析)----用户事务分析是站在用户角度进行的基础性能分析. Transation Sunmmary(事务综述)----对事务进行综合分析是性能分析的第一步,通过分析 ...
- linux 常用find命令
1.查找当前目录下以test开头的所有文件-会进入子目录中去查找 [root@rusky hgfs]# find -name test* 2.查找当前目录下名为test.txt的文件-会进入子目录中去 ...
- Linux能力(capability)机制的继承
1.Linux能力机制概述 在以往的UNIX系统上,为了做进程的权限检查,把进程分为两类:特权进程(有效用户ID是0)和非特权进程(有效用户ID是非0).特权进程可以通过内核所有的权限检查,而非特权进 ...