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的更多相关文章

  1. [LeetCode] Text Justification 文本左右对齐

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  2. 【leetcode】Text Justification

    Text Justification Given an array of words and a length L, format the text such that each line has e ...

  3. 【leetcode】Text Justification(hard) ☆

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  4. LeetCode(68) Text Justification

    题目 Given an array of words and a length L, format the text such that each line has exactly L charact ...

  5. LeetCode:Text Justification

    题目链接 Given an array of words and a length L, format the text such that each line has exactly L chara ...

  6. 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 ...

  7. 68. Text Justification

    题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...

  8. leetcode@ [68] Text Justification (String Manipulation)

    https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...

  9. juce Justification 分析

    很简单的一个类,一个rect放置在另一个rect中如何放置.只是没有考虑边距等,估且认为是在外层作考虑吧.然后认为是外框比内框大,所以外层怕是要进行检查才行 #ifndef JUCE_JUSTIFIC ...

随机推荐

  1. cocos2d-x 头文件中添加方法变量导致编译报错

    代码如下: HelloWorldScene.h #ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__ #include " ...

  2. 2014-08-13 SQL语句之Left Join

    今天是在吾索实习的第26天.这天在处理数据库数据的时候发现了一个不错的语句就是Left Join,即左连接.  其功能是:即使右表中没有匹配,也从左表返回所有的行.也就是说,显示的行数与左表一致,且当 ...

  3. Windows服务器Pyton辅助运维--03.安装Visual Studio 的 Python 开发插件 PTVS

    PTVS (Python Tools for Visual Studio) http://pytools.codeplex.com/ 当前版本:2.1 RC PTVS (Python Tools fo ...

  4. hdu-1800

    思路: 这题被坑的不轻. 首先花了一段时间想明白了思路是要找出现次数最多数字,以为这题就这样解决了,结果发现每个数字的最大长度是30,long long都装不下,因此就要用字符串来保存处理.然后在in ...

  5. mysql优化整理(索引)

    什么是索引? 索引是表记录的单个或多个字段重新组织的一种方法,其目的是提高数据库的查询速度,本质上就是一种数据结构. 索引的类型:primary(主键).secondary(其他) 索引的数据结构 I ...

  6. 【KMP】Number Sequence

    KMP算法 KMP的基处题目,数字数组的KMP算法应用. 主要是next[]数组的构造,next[]存储的是字符的当前字串,与子串前字符匹配的字符数. 移动位数 = 已匹配的字符数 - 对应的部分匹配 ...

  7. jQuery扩展与noConflict的用法-小示例

    有时我们要用到自己定义的jquery,这时可以通过jQuery扩展来实现该功能 index.html <!DOCTYPE html> <html> <head> & ...

  8. JS 中的引用

    首先有一个全局变量  JsonArry={"key":"value"}; 假设这个object为{"你好":"引用"} ...

  9. Mina入门实例

    继续上一篇,这篇主要讲通过mina往B端发送消息.并接受消息,mina是一个网络通信框架,封装了javaNIO.简单易用.网上有非常多关于他的介绍,在此不赘述了. 如上篇所介绍,完毕功能,须要五个类: ...

  10. Unity用户自定义圆角头像

    前天朋友遇到一个这样的需求,而且比较棘手让我帮忙解决.需求就是棋牌类的游戏,玩家的个人资料中包括自己的头像而且可以浏览相册中的图片或者使用相机拍照设置.关于这个问题我也查阅一些资料,由于涉及安卓部分知 ...