68. Text Justification *HARD*
2016-03-13 11:57
274 查看
GivenanarrayofwordsandalengthL,formatthetextsuchthateachlinehasexactlyLcharactersandisfully(leftandright)justified.
Youshouldpackyourwordsinagreedyapproach;thatis,packasmanywordsasyoucanineachline.Padextraspaces
Extraspacesbetweenwordsshouldbedistributedasevenlyaspossible.Ifthenumberofspacesonalinedonotdivideevenlybetweenwords,theemptyslotsontheleftwillbeassignedmorespacesthantheslotsontheright.
Forthelastlineoftext,itshouldbeleftjustifiedandnoextraspaceisinsertedbetweenwords.
Forexample,
words:
L:
Returntheformattedlinesas:
Note:EachwordisguaranteednottoexceedLinlength.
clicktoshowcornercases.
CornerCases:
Alineotherthanthelastlinemightcontainonlyoneword.Whatshouldyoudointhiscase?
Inthiscase,thatlineshouldbeleft-justified.
测试用例:
["a","b","c","d","e"]1--["a","b","c","d","e"]
["Listen","to","many,","speak","to","a","few."]6--["Listen","to","many,","speak","toa","few."]
["Here","is","an","example","of","text","justification."]16--
["Don't","go","around","saying","the","world","owes","you","a","living;","the","world","owes","you","nothing;","it","was","here","first."]
30--
Youshouldpackyourwordsinagreedyapproach;thatis,packasmanywordsasyoucanineachline.Padextraspaces
''whennecessarysothateachlinehasexactlyLcharacters.
Extraspacesbetweenwordsshouldbedistributedasevenlyaspossible.Ifthenumberofspacesonalinedonotdivideevenlybetweenwords,theemptyslotsontheleftwillbeassignedmorespacesthantheslotsontheright.
Forthelastlineoftext,itshouldbeleftjustifiedandnoextraspaceisinsertedbetweenwords.
Forexample,
words:
["This","is","an","example","of","text","justification."]
L:
16.
Returntheformattedlinesas:
[ "Thisisan", "exampleoftext", "justification." ]
Note:EachwordisguaranteednottoexceedLinlength.
CornerCases:
Alineotherthanthelastlinemightcontainonlyoneword.Whatshouldyoudointhiscase?
Inthiscase,thatlineshouldbeleft-justified.
classSolution{ public: vector<string>fullJustify(vector<string>&words,intmaxWidth){ intn=words.size(),l,extra,eachex,i,j,k,t; vector<int>len(n,0); for(i=0;i<n;i++) len[i]=words[i].length(); vector<string>ans; i=0; while(i<n) { for(l=len[i],j=i+1;j<n&&l<=maxWidth;j++) l+=len[j]+1; strings=words[i]; if(j==n&&l<=maxWidth)//thelastline { for(k=i+1;k<j;k++) { s+=''; s+=words[k]; } } else { j--; l-=len[j]+1; extra=maxWidth-l; eachex=(j>i+1)?extra/(j-i-1):extra; for(k=i+1;k<j;k++) { if(extra) { if((j==i)||(extra==eachex*(j-k))) { for(t=0;t<eachex;t++) s+=''; extra-=eachex; } else { for(t=0;t<eachex+1;t++) s+=''; extra-=eachex+1; } } s+=''; s+=words[k]; } } for(k=s.length();k<maxWidth;k++) s+=''; ans.push_back(s); i=j; } returnans; } };
测试用例:
["a","b","c","d","e"]1--["a","b","c","d","e"]
["Listen","to","many,","speak","to","a","few."]6--["Listen","to","many,","speak","toa","few."]
["Here","is","an","example","of","text","justification."]16--
["Hereisan","exampleoftext","justification."]
["Don't","go","around","saying","the","world","owes","you","a","living;","the","world","owes","you","nothing;","it","was","here","first."]
30--
["Don'tgoaroundsayingthe","worldowesyoualiving;the","worldowesyounothing;itwas","herefirst."]
相关文章推荐
- HBase中Region, store, storefile和列簇的关系
- 构建之法阅读笔记02
- 浅析操作系统----2、操作系统概述
- code1316 文化之旅
- 并查集
- 20135316王剑桥Linux内核学习笔记第三周
- 缓存数据库Memcached
- Django 学习笔记一
- 泛型方法Demo
- crontab——Linux 下的定时任务
- 第三周项目三 输出图形
- OpenCV学习笔记_用指针操作图像元素
- POJ 3169 Layout(差分约束啊)
- 开源代码托管
- 旋转方法
- c# 之下如何做定时
- iOS开发笔记--什么时候调用layoutSubviews
- Maven 常规错误一览及解决方案
- mac自带的sed和linux不一致, 需要安装gnu-sed
- linux及安全第三周总结——跟踪分析LINUX内核的启动过程