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 ...
随机推荐
- php敏感词过滤
在项目开发中发现有个同事在做敏感词过滤的时候用循环在判断,其实是不用这样做的,用php的数组函数和字符串函数即可实现 function filterNGWords($string) { $badwor ...
- Library工程No resource identifier found for attribute
使用library工程中自定义属性无法识别问题 解决:xmlns:ptr="http://schemas.android.com/apk/res/包名, 改成xmlns:ptr=" ...
- SQL Server 基础:拾遗
1.一条完整的sql语句: select top | distinct 字段, 表达式, 函数, ... from 表表达式 where 筛选条件 group by 分组条件 having 筛选条件 ...
- 没有Where条件下group by走索引
C:\Users\Administrator>sqlplus /nolog SQL :: Copyright (c) , , Oracle. All rights reserved. SQL&g ...
- 第五节:什么导致Finalize方法被调用
Finalize方法在垃圾回收结束时被调用,下面有5种事件会导致开始垃圾回收 1.第0代已满 第0代已满,垃圾回收会自动开始.该事件是目前导致Finalize方法被调用的最常见的一种方式,因为虽 ...
- 菜鸟学习Spring——60s使用annotation实现简单AOP
一.概述. AOP大家都知道切面编程,在Spring中annotation可以实现简单的AOP列子.下面还未大家介绍几个概念: Aspect 对横切性关注点的模块化. Advice 对横切性关注点的具 ...
- [JAVA][RCP] Eclipse4/RCP/Lifecycle
E4AP provides two levels of lifecycles, for contributions and for the application. Contents [hide] ...
- android获取手机录
在Android开发中,读取手机通讯录中的号码是一种基本操作,但是由于Android的版本众多,所以手机通讯录操作的代码比较纷杂,在本文中进行一下总结. Android1.5是现在的Android系统 ...
- git的初步使用
1.在GitHub上建立项目登录GitHub后,你可以在右边靠中那里找到一个按钮“New Repository”,点击过后,填入项目名称.说明和网址过后就可以创建了,然后会出现一个提示页面,记下类似g ...
- 今天开始应该使用 5 个JavaScript调试技巧
原文:5 Javascript debugging tips you’ll start using today 我之前使用过用 printf debugging,自此之后我用这种方法似乎总能更快地解决 ...