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

  1. Text Justification leetcode java

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

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

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

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

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

  4. 【一天一道LeetCode】#68. Text Justification

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. [leetcode]Text Justification @ Python

    原题地址:https://oj.leetcode.com/problems/text-justification/ 题意: Given an array of words and a length L ...

  6. LeetCode OJ——Text Justification

    http://oj.leetcode.com/problems/text-justification/ 编译代码要看warnings!它提供了可能出问题的情况,比如类型转换上unsigned int ...

  7. [LeetCode] 68. Text Justification 文本对齐

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

  8. 【leetcode】Text Justification

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

  9. 【leetcode】Text Justification(hard) ☆

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

随机推荐

  1. Oracle重置过期的密码

    过期的原因一般有两种可能: 一.由于Oracle 11g在默认的default概要文件中设置了“PASSWORD_LIFE_TIME=180”天导致:   这种情况的解决办法: 1.查看用户的proi ...

  2. [UVa1210]Sum of Consecutive Prime Numbers(前缀和,打表)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  3. CUBRID学习笔记 15 Lobs类型数据

    BLOB: Binary large object CLOB: Character large object 一个二进制 一个字符类型 二进制的读取 CUBRIDCommand cmd = new C ...

  4. How to crack interviews ...

    Code practice: Leetcode: www.leetcode.com HackerRank: www.hackerrank.com Topcoder: https://www.topco ...

  5. C#强制清除缓存

    #region 强制清除缓存 [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize" ...

  6. Python基础学习笔记(十二)文件I/O

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-files-io.html ▶ 键盘输入 注意raw_input函 ...

  7. ubuntu下安装gradle

    1.下载gradle 下载地址:当前版本gradle-2.10-all.zip http://gradle.org/gradle-download/ 2.解压安装gradle unzip gradle ...

  8. C#垃圾回收机制(GC)

    GC的前世与今生 虽然本文是以.net作为目标来讲述GC,但是GC的概念并非才诞生不久.早在1958年,由鼎鼎大名的图林奖得主John McCarthy所实现的Lisp语言就已经提供了GC的功能,这是 ...

  9. yii CDbCriteria 类的总结

    在编程中,我们通常会需要查询些东西,但是通过查询的时候,yii有个集成的类--- CDbCriteria():通过该类,我们可以更加便捷的调用数据. 参考网址:http://www.cnblogs.c ...

  10. 使用xml来显示获取的mysql数据

    mysql test -u test -X -e 'select * from employees where empid = 1' 其中 -X 就是以xml形式显示