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