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 ...
随机推荐
- java模拟http post
我们将使用java.net.URLConnection来完成一次post请求,假设要post数据到http://localhost:8080/doSome上: URLConnection urlCon ...
- Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数
E. Infinite Inversions ...
- Java实现生命周期管理机制
先扯再说 最近一直在研究某个国产开源的MySQL数据库中间件,拉下其最新版的代码到eclipse后,启动起来,然后做各种测试和代码追踪:用完想要关闭它时,拉出它的STOP类想要运行时,发现这个类里赫然 ...
- [Flask]学习Flask第三天笔记总结
from flask import Flask,render_template,request from others import checkLogin app = Flask(__name__) ...
- 【HPP开发】让所有中小企业拥有自己的APP
HPP hybirdApp或者hbuilderApp, 指通过html,css,js语言开发出ios和android两个版本的APP, 开发效率成倍上升,开发时间大幅缩减,开发成本同样也大大缩减. 移 ...
- iOS中block实现的探究
[0. Brief introduction of block] Block是iOS4.0+ 和Mac OS X 10.6+ 引进的对C语言的扩展,用来实现匿名函数的特性. 用维基百科的话来说,Blo ...
- [React Testing] Children with Shallow Rendering
When testing React components, we often want to make sure the rendered output of the component match ...
- 每天进步一点点——Linux系统时间来处理
转载请注明出处:http://blog.csdn.net/cywosp/article/details/25839551 在程序中时间处理往往是一个麻烦的事.Linux系统提供了非常多关于时间处理的函 ...
- linux下CDROM挂载
在VM-->removableDevice-->CD DVD-->加载iso镜像文件: [root@rusky2 mnt]# mount /dev/cdrom /mnt/cdrom ...
- hdu 1076
水题 AC代码: #include <iostream> using namespace std; int main() { int i,k,t,y,n; cin>>t; wh ...