Text Justification [LeetCode]
Problem Description:http://oj.leetcode.com/problems/text-justification/
Note: Just be careful about boundary when implementing it.
class Solution {
public:
string genNormalLine(vector<string> &words, int L, int start, int end){
int word_count = end - start + ;
string line;
if(word_count == ){
line.append(words[start]);
line.append(L-line.size(), ' ');
}else{
int slot_num = word_count - ;
int words_size = ;
for(int i = start; i <= end; i++)
words_size += words[i].size();
int space_num = L - words_size;
int reminder = space_num % slot_num;
int quotient = space_num / slot_num;
line.append(words[start]);
for(int i = start + ; i <= end; i++) {
if(reminder > ){
line.append(quotient + , ' ');
reminder --;
} else {
line.append(quotient, ' ');
}
line.append(words[i]);
}
}
return line;
}
vector<string> fullJustify(vector<string> &words, int L) {
// Note: The Solution object is instantiated only once and is reused by each test case.
vector<string> formatted_lines;
if(words.size() == )
return formatted_lines;
for(int i = ; i < words.size();){
int length = ;
int j = i;
for(; j < words.size(); j ++){
if( j == i)
length += words[j].size();
else
length += (+ words[j].size());
if(length > L)
break;
}
//put words in a line
if(j == words.size()) {
//last line
string line;
line.append(words[i]);
for(int k = i + ; k < words.size(); k++){
line.push_back(' ');
line.append(words[k]);
}
if(line.size() < L){
line.append(L-line.size(), ' ');
}
formatted_lines.push_back(line);
}else {
//normal line
formatted_lines.push_back(genNormalLine(words,L, i, j - ));
}
//next start index
i = j;
}
return formatted_lines;
}
};
Text Justification [LeetCode]的更多相关文章
- Text Justification leetcode java
题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...
- [LeetCode] Text Justification 文本左右对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- leetcode@ [68] Text Justification (String Manipulation)
https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...
- 【一天一道LeetCode】#68. Text Justification
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [leetcode]Text Justification @ Python
原题地址:https://oj.leetcode.com/problems/text-justification/ 题意: Given an array of words and a length L ...
- LeetCode OJ——Text Justification
http://oj.leetcode.com/problems/text-justification/ 编译代码要看warnings!它提供了可能出问题的情况,比如类型转换上unsigned int ...
- [LeetCode] 68. 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 ...
随机推荐
- Cheatsheet: 2014 04.01 ~ 04.30
Java 115 Java Interview Questions and Answers – The ULTIMATE List 3 Good Reasons to Avoid Arrays in ...
- linux bash shell 流程控制(if/else )
本文转自:http://blog.csdn.net/flowingflying/article/details/5069646 本文也即<Learning the bash Shell>3 ...
- Django 分页2 (Pagination)
分页是Web应用常用的手法,Django提供了一个分页器类Paginator(django.core.paginator.Paginator),可以很容易的实现分页的功能.该类有两个构造参数,一个是数 ...
- vim使用札记
最近开始用vim编辑器了,从最开始的配置到现在慢慢使用,我在这儿会贴出一些我的使用上遇到过的问题和如何解决的方案,留给自己和一些会用到的人看看 1.vim怎么使汇编语法高亮 开始不知道,然后把文件的后 ...
- ABAP RFC远程调用
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- CUBRID学习笔记 25 数据类型2
---恢复内容开始--- 6枚举类型 语法 <enum_type> : ENUM '(' <char_string_literal_list> ')' <char_str ...
- 分区格式化mkfs
mkfs — build a Linux filesystem 用法举例: mkfs.ext3 /dev/sdb1 #把sdb1格式化为ext3文件系统 也可以写成 mkfs -t ext3 /d ...
- OnClientClick的用法
摘自:http://blog.csdn.net/coolpig86/article/details/5439560 OnClientClick用于执行客户端脚本.当我们单击一个按钮时,最先执行的是On ...
- 一致性 hash 算法( consistent hashing )
consistent hashing 算法早在 1997 年就在论文 Consistent hashing and random trees 中被提出,目前在cache 系统中应用越来越广泛: 1 基 ...
- linux安装phpstorm出现 Startup Error: Unable to detect graphics environment
在centos6.2下在安装phpstorm下遇到 Startup Error: Unable to detect graphics environment 其中jdk安装的版本是 1.7.0_79 ...