Leetcode#68 Text Justification
没有复杂的算法,纯粹的模拟题
先试探,计算出一行能放几个单词
然后计算出单词之间有几个空格,注意,如果空格总长度无法整除空格数,前面的空格长度通通+1
最后放单词、放空格,组成一行,加入结果中
对于最后一行要特殊处理
代码:
vector<string> fullJustify(vector<string> &words, int L) {
vector<string> text;
int n = words.size(); int i = ;
int j = ; while ((i = j) < n) {
int wordsLen = ;
// 试探
while (j < n && wordsLen + words[j].length() + j - i <= L) {
wordsLen += words[j].length();
j++;
}
// 特殊处理最后一行
if (j == n) {
string line = words[i];
for (int k = i + ; k < j; k++)
line += " " + words[k];
line += string(L - wordsLen - (j - i - ), ' ');
text.push_back(line);
break;
} if (j == i + ) // 只有一个单词的行也单独处理,避免除0
text.push_back(words[j - ] + string(L - words[j - ].length(), ' '));
else { // 普通情况
int padLen = (L - wordsLen) / (j - i - );
int remainNum = L - wordsLen - padLen * (j - i - );
string line = words[i];
for (int k = i + ; k < j; k++) {
string pad(padLen, ' ');
if (remainNum > ) {
pad += " ";
remainNum--;
}
line += pad + words[k];
}
text.push_back(line);
}
} return text;
}
Leetcode#68 Text Justification的更多相关文章
- leetcode@ [68] Text Justification (String Manipulation)
https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...
- [LeetCode] 68. Text Justification 文本对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- [leetcode]68. Text Justification文字对齐
Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...
- 【一天一道LeetCode】#68. Text Justification
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】68. Text Justification
Text Justification Given an array of words and a length L, format the text such that each line has e ...
- 68. Text Justification
题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...
- LeetCode OJ——Text Justification
http://oj.leetcode.com/problems/text-justification/ 编译代码要看warnings!它提供了可能出问题的情况,比如类型转换上unsigned int ...
- 【leetcode】Text Justification
Text Justification Given an array of words and a length L, format the text such that each line has e ...
- 【leetcode】Text Justification(hard) ☆
Given an array of words and a length L, format the text such that each line has exactly L characters ...
随机推荐
- Linux基础知识-文件管理
Linux目录与路径 cd:切换目录 例如:cd ~willhua,则回到用户willhua的主文件夹 cd ~或者cd,则表示回到自己的的主文件夹 cd -,则表示回到上个目录 pwd:显示目前 ...
- PHP系统函数
(一)字符串处理函数 Chr函数 作用:根据ASCII码返回相应的字符. 语法:string chr(int ascii): Chop函数 作用:去除字符串中连续空格和空白行. 语法:string c ...
- (转)Android L Ripple的使用
声明:Demo并不是有本人所写,本人只是总结在这里 工程源码: RippleDemo.zip ---------------------------------------------------- ...
- 【转】代码编辑器(一)-TSynCompletionProposal用法
注意,本系列均转载自http://blog.163.com/zom1995@126/ 网上有人给我一个SynEdit这个东西,因为我很喜欢自己编个代码编辑器,但要是用Delphi直接弄的,就我现在这样 ...
- Python初学者笔记:打印出斐波那契数列的前10项
问题:斐波那契数列(意大利语: Successione di Fibonacci),又称黄金分割数列.费波那西数列.费波拿契数.费氏数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21.- ...
- openstack的第二天
今天,在公司测试了还是网络有问题. 但是用了rdo还是成功了~明天就再试试怎么开放端口进来.
- the usage of key word "static" in java language
---恢复内容开始--- 作用 有时你希望定义一个类成员,使它的使用完全独立于该类的任何对象.通常情况下,类成员必须通过它的类的对象访问,但是可以创建这样一个成员,它能够被它自己使用,而不必引用特定的 ...
- 浅谈iOS网络编程之一入门
计算机网络,基本上可以抽象是端的通信.实际在通讯中会用到不同的设备,不同的硬件中,为了能友好的传输信息,那么建立一套规范就十分必要了.先来了解一些基本概念 了解网络中传输的都是二进制数据流. 2.了 ...
- SQL Server基本操作积累
一.基本操作 1.将数据绑定到DataGridVirw控件上显示的数据列标题将会是数据库中的字段名称,可以在使用select语句时使用AS关键字将转化为列名的别名 select name AS 姓名 ...
- C# 堆和栈的区别-该文转自:http://www.itcodes.cn/746.html | 程序人生
理解堆与栈对于理解.NET中的内存管理.垃圾回收.错误和异常.调试与日志有很大的帮助.垃圾回收的机制使程序员从复杂的内存管理中解脱出来,虽然绝大多数的C#程序并不需要程序员手动管理内存,但这并不代表程 ...