【Text Justification】cpp
2015-06-09 22:43
344 查看
题目:
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
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:
L:
Return the formatted lines as:
Note: Each word is guaranteed not to exceed L in length.
click to show corner cases.
Corner Cases:
A line other than the last line might contain only one word. What should you do in this case?
In this case, that line should be left-justified.
代码:
tips:
1. 要对string的各种操作都很熟悉
2. 要理解对题意,重点是到底啥是evenly:意思是不能evenly,就从左边到右一个个分配。
3. 扫一扫各种test case,多扫几遍可以AC了。
这道题自己扫了4次,终于AC了;经验就是,如果不能做到第一次把所有关键细节都考虑完全了,至少把一些细节(诸如blank_num,pos_num,blank_remain)单独列拎出来,这样做的好处就是如果遇上各种需要考虑的corner cases可以单独处理这些关键细节,而不用影响其他的部分。
====================================================
第二次过这道题,算是写过的leetcode中最繁琐的之一了,欣慰的是一次AC了。
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.
click to show corner cases.
Corner Cases:
A line other than the last line might contain only one word. What should you do in this case?
In this case, that line should be left-justified.
代码:
class Solution { public: vector<string> fullJustify(vector<string>& words, int maxWidth) { vector<string> ret; vector<string> tmp; // words in one line int width = 0; // total width in one line const char BLANK=' '; int i=0; while ( i<words.size() ) { // judge if a word can be added in a line if ( width+words[i].size() <= maxWidth ) { if ( width+words[i].size()==maxWidth ) { tmp.push_back(words[i]); width = maxWidth; } else { tmp.push_back(words[i]+string(1,BLANK)); width += words[i].size()+1; } } // or create a new line & handle the words[i] else { // CREAT A NEW LINE // number of blank need to add int blank_num = maxWidth - width; if ( tmp.back()[tmp.back().size()-1]==BLANK ) { tmp.back() = string(tmp.back().begin(),tmp.back().end()-1); blank_num = blank_num + 1; } // number of position for blanks int pos_num = tmp.size()-1; if ( pos_num==0 ) { ret.push_back(tmp.back()+string(blank_num, BLANK)); } else { // number of blanks remain to be evenly distributed int blank_remain = blank_num % pos_num; for ( int j=0; j<tmp.size()-1; ++j ) { tmp[j] = tmp[j]+ string(blank_num/pos_num, BLANK); } for ( int j=0; j<blank_remain; ++j ) { tmp[j] = tmp[j] + string(1, BLANK); } string str = ""; for ( int j=0; j<tmp.size(); ++j ) str += tmp[j]; ret.push_back(str); } // HANDLE THE words[i] tmp.clear(); if ( words[i].size()==maxWidth ) { tmp.push_back(words[i]); width = maxWidth; } else { tmp.push_back(words[i]+string(1, BLANK)); width = words[i].size()+1; } } ++i; } // address the remain line if ( tmp.size()!=0 ) { int blank_num = maxWidth - width; string last_line = ""; for ( int i=0; i<tmp.size(); ++i ) last_line += tmp[i]; ret.push_back(last_line+string(blank_num, BLANK)); } return ret; } };
tips:
1. 要对string的各种操作都很熟悉
2. 要理解对题意,重点是到底啥是evenly:意思是不能evenly,就从左边到右一个个分配。
3. 扫一扫各种test case,多扫几遍可以AC了。
这道题自己扫了4次,终于AC了;经验就是,如果不能做到第一次把所有关键细节都考虑完全了,至少把一些细节(诸如blank_num,pos_num,blank_remain)单独列拎出来,这样做的好处就是如果遇上各种需要考虑的corner cases可以单独处理这些关键细节,而不用影响其他的部分。
====================================================
第二次过这道题,算是写过的leetcode中最繁琐的之一了,欣慰的是一次AC了。
class Solution { public: vector<string> fullJustify(vector<string>& words, int maxWidth) { vector<string> ret; vector<string> tmp; int currWidth = 0; for ( int i=0; i<words.size()-1; ++i ) { // no room for curr word if ( currWidth+words[i].size()>maxWidth ) { // address words already in tmp int blank = maxWidth - currWidth + 1; tmp.back() = tmp.back().substr(0,tmp.back().size()-1); if ( tmp.size()>1 ){ int even = blank/(tmp.size()-1); for ( int k=0; k<tmp.size()-1; k++ ) tmp[k] = tmp[k] + string(even,' '); int remain = blank%(tmp.size()-1); for ( int k=0; k<remain; k++ ) tmp[k] = tmp[k] + " "; string str = ""; for ( int k=0; k<tmp.size(); ++k ) str += tmp[k]; ret.push_back(str); } else{ tmp[0] = tmp[0]+string(blank,' '); ret.push_back(tmp[0]); } tmp.clear(); // consider words[i]'s width if ( words[i].size()<maxWidth ) { tmp.push_back(words[i]+" "); currWidth = words[i].size()+1; } else{ ret.push_back(words[i]); currWidth = 0; } } // room for current word else { // no room for extra blank if ( currWidth+words[i].size()==maxWidth ) { tmp.push_back(words[i]); string str = ""; for ( int k=0; k<tmp.size(); ++k ) str += tmp[k]; ret.push_back(str); tmp.clear(); currWidth = 0; } // room for extra blank else { tmp.push_back(words[i]+" "); currWidth += words[i].size()+1; } } } // last line if ( currWidth+words[words.size()-1].size()>maxWidth ) { // address words already in tmp int blank = maxWidth - currWidth + 1; tmp.back() = tmp.back().substr(0,tmp.back().size()-1); if ( tmp.size()>1 ){ int even = blank/(tmp.size()-1); for ( int k=0; k<tmp.size()-1; k++ ) tmp[k] = tmp[k] + string(even,' '); int remain = blank%(tmp.size()-1); for ( int k=0; k<remain; k++ ) tmp[k] = tmp[k] + " "; string str = ""; for ( int k=0; k<tmp.size(); ++k ) str += tmp[k]; ret.push_back(str); } else{ tmp[0] = tmp[0]+string(blank,' '); ret.push_back(tmp[0]); } // address the last line blank = maxWidth - words[words.size()-1].size(); ret.push_back(words[words.size()-1]+string(blank,' ')); } else { tmp.push_back(words[words.size()-1]+string(maxWidth-currWidth-words[words.size()-1].size(),' ')); string str = ""; for ( int k=0; k<tmp.size(); ++k ) str += tmp[k]; ret.push_back(str); } return ret; } };
相关文章推荐
- C++中如何避免内存泄露
- 对C++中顺序容器的插入迭代器的认识
- c语言中scanf等函数关于输入流缓冲区
- C++ bool值
- C语言基础练习:在Xcode中练习的for循环语句
- C++继承时派生类与基类有同名函数时如何分别引用
- zerglurker的C语言教程004——指针初步讲解
- Java基础 Java与C++的几点细节不同
- ###《More Effective C++》- 操作符
- C++归并算法
- 某整形数组中除了两个单身整数外, 其余的整数都是成对出现的, 利用C/C++代码求出这两个单身整数。 要求: 时间复杂度o(n), 空间复杂度o(1)------某公司招聘试题
- C++中 引入虚基类的作用
- C++内存对齐
- 【学习笔记】【C语言】选择结构-if
- C++类 (03)
- 实现Android Studio JNI开发C/C++使用__android_log_print输出Log
- C++ Primer 学习笔记_40_STL实践与分析(14)--概要、先来看看算法【上】
- 2015届华为校园招聘机试题 C语言实现
- 【学习笔记】【C语言】流程控制
- c语言称重砝码