https://oj.leetcode.com/problems/text-justification/

细节题

class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L) {
vector<string> ans; if(L == )
{
ans.push_back("");
return ans;
} int index = ;
int begin = ;
int tempLen = ;
int end = ;
int spaces = ;
int extraSpace = ;
while(index < words.size())
{
begin = index;
tempLen = words[index].size();
index++;
while(index < words.size() && (tempLen + + words[index].size()) <= L)
{
tempLen = tempLen + + words[index].size();
index++;
}
end = index - ;
string str;
// not the last line
if(end != words.size() - )
{
// has more than one word
if(end != begin)
{
spaces = (L - tempLen) / (end - begin);
extraSpace = (L - tempLen) % (end - begin);
string strSpace;
for(int i = ; i < spaces + ; i++)
strSpace.append(" ");
for(int p = begin; p < end; p++)
{
str.append(words[p]);
str.append(strSpace);
if(p - begin < extraSpace)
str.append(" ");
}
str.append(words[end]);
}
else
{
str.append(words[begin]);
while(str.size() < L)
str.append(" ");
}
}
else
{
for(int p = begin; p < end; p++)
{
str.append(words[p]);
str.append(" ");
}
str.append(words[end]);
while(str.size() < L)
str.append(" ");
}
ans.push_back(str);
}
return ans;
}
};

最新文章

  1. 【翻译】首个基于NHibernate的应用程序
  2. android释放内存的一个办法
  3. Vs 2015 调试ASP.NET Core修改监听端口
  4. 谷歌浏览器安装adblock广告屏蔽插件
  5. 小白学习mysql之函数
  6. utf8 和 UTF-8 的区别
  7. Oracle中的IF...THEN...ELSE判断
  8. Keil C51 与 ARM 并存的方法
  9. 探索Oracle数据库升级6 11.2.0.4.3 Upgrade12c(12.1.0.1)
  10. ATMEGA16 IOport相关汇总
  11. Cent OS 6.5下源码安装php7.2
  12. Android Studio使用过程中遇到的错误
  13. 【docker简易笔记】docker基础信息的分享
  14. T-SQL动态查询(3)——静态SQL
  15. 基于 HTML5 的 WebGL 自定义 3D 摄像头监控模型
  16. 与python的三天
  17. Jmeter监控服务器笔记
  18. 【XSY2754】求和 莫比乌斯反演 杜教筛
  19. Oracle外部表详解
  20. WEB上传大文件解决方案

热门文章

  1. 关于web.xml配置中的&lt;url-pattern&gt;
  2. Python入门必学:字符串和编码正确的使用方法
  3. Head First Python (一)
  4. linux shell 单双引号区别
  5. Network of Schools POJ - 1236 (强联通)
  6. 扩展程序 - Google Chrome
  7. P2615 神奇的幻方
  8. java高级编程技巧
  9. Python中函数参数类型和参数绑定
  10. Leetcode 525.连续数组