【leetCode】Text Justification
来源:互联网 发布:倒卖数据什么罪 编辑:程序博客网 时间:2024/06/05 05:23
题意:给定一组单词序列和一个最大的单词长度,要求按行显示单词,单词之间有空格,每行的最大长度不能超过给定值。对于非末行,空格的长度均匀分配,对于末行,左对齐显示,即单词之间只能有一个空格,剩余的空格在最后一个单词之后。
思路:没什么特别的,纯模拟题。
代码:
vector<string> fullJustify(vector<string>& words, int maxWidth){ vector<int> length; vector<string> ans; int i,j,k,last,total,remain,a,ns,size=words.size(); if(size==0 ) return ans; for(i=0; i<size; i++) length.push_back(words[i].length()); last=0; total=0; for(i=0; i<size; i++) { total=total+words[i].length()+(i==last?0:1); if(total>maxWidth) { cout<<i<<endl; total=0; for(k=last; k<i; k++)total+=words[k].length(); remain=maxWidth-total; i--; string temp=""; for(k=last; k<=i; k++) { temp+=words[k]; if(k!=i) { ns=remain/(i-k); if(remain%(i-k)!=0) ns++; for(j=0; j<ns; j++) temp+=" "; remain=remain-ns; } } for(k=0;k<remain;k++) temp+=" "; ans.push_back(temp); total=0; last=i+1; } else if(i==size-1) { total=0; for(k=last; k<=i; k++)total+=words[k].length(); remain=maxWidth-total; string temp=""; for(k=last; k<=i; k++) { temp+=words[k]; if(remain>0)temp+=" "; remain--; } for(k=0;k<remain;k++) temp+=" "; ans.push_back(temp); } } return ans;}
0 0
- LeetCode : Text Justification
- [LeetCode] Text Justification
- leetcode 103: Text Justification
- [LeetCode]Text Justification
- [leetcode] Text Justification
- Text Justification leetcode
- Leetcode: Text Justification
- leetcode Text Justification
- LeetCode Text Justification
- LeetCode | Text Justification
- LeetCode Text Justification
- [Leetcode] Text Justification (Java)
- LeetCode OJ:Text Justification
- Leetcode: Text Justification
- [LeetCode] Text Justification
- 【LeetCode】Text Justification
- Text Justification -- LeetCode
- LeetCode: Text Justification [068]
- Eclipse警告解决:Implementation of project facet maven could not be found. Functionality will be limited.
- 一款猥琐的PHP后门分析
- Uncaught TypeError: Super expression must either be null or a function, not undefine
- SSM_config配置springmvc.xml模板
- linux安装MySQL5.7.13(二进制|源码)
- 【leetCode】Text Justification
- Java 8 lambda初试
- service与thread
- AnyRTC全新开源项目助力直播
- [Leetcode] Binary Tree Level Order Traversal
- 子网掩码详解
- C#word转pdf System.Runtime.InteropServices.COMException(0x80040202):导出失败,因为此功能尚未安装
- 圆形头像制作,仿QQ做法。
- View的绘制