原题地址

没有复杂的算法,纯粹的模拟题

先试探,计算出一行能放几个单词

然后计算出单词之间有几个空格,注意,如果空格总长度无法整除空格数,前面的空格长度通通+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的更多相关文章

  1. leetcode@ [68] Text Justification (String Manipulation)

    https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...

  2. [LeetCode] 68. Text Justification 文本对齐

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  3. [leetcode]68. Text Justification文字对齐

    Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...

  4. 【一天一道LeetCode】#68. Text Justification

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. 【LeetCode】68. Text Justification

    Text Justification Given an array of words and a length L, format the text such that each line has e ...

  6. 68. Text Justification

    题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...

  7. LeetCode OJ——Text Justification

    http://oj.leetcode.com/problems/text-justification/ 编译代码要看warnings!它提供了可能出问题的情况,比如类型转换上unsigned int ...

  8. 【leetcode】Text Justification

    Text Justification Given an array of words and a length L, format the text such that each line has e ...

  9. 【leetcode】Text Justification(hard) ☆

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

随机推荐

  1. Linux命令学习---目录

    一.文件相关命令 1.文件显示 1)tail,head,more,less,cat,nl

  2. canvas绘制文字

    绘制字体时可以使用fillText方法或者strokeText方法. fillText方法用填充的方式来绘制字符串 context.fillText (text, x,y,[maxwidth]); s ...

  3. php中iconv函数使用方法

    最近在做一个程序,需要用到iconv函数把抓取来过的utf-8编码的页面转成gb2312, 发现只有用iconv函数把抓取过来的数据一转码数据就会无缘无故的少一些. iconv函数库能够完成各种字符集 ...

  4. html中的框架

    1.总的代码 <frameset rows=20%,*> <frame src="top.html" /> <frameset cols=30%,*& ...

  5. Ubuntu kylin系统改中文系统文件名为英文

    刚装好系统,将使用语言改成了中文,结果重启后,提示是否将文件系统的名字改为新的,我一不注意,点了是...这样,在以后使用终端的时候,会有中文来干扰,所以需要改回英文. 方法如下: 输入两个命令即可: ...

  6. Python核心编程--学习笔记--4--Python对象

    现在开始学习Python语言的核心部分.首先了解什么是Python对象,然后讨论最常用的内建类型,接下来讨论标准类型运算符和内建函数,之后给出对标准类型的不同分类方式,最后提一提Python目前还不支 ...

  7. Ajax-goahead局部刷新页面

    软件开发最常用的方法是:C/S,B/S.如果嵌入式设备中使用Ajax,那么既可以使用C/S方式,也可以使用B/S开发上位机.最近公司的一个项目需要异步获取后台数据,使用form更新数据时会有空白卡顿不 ...

  8. C语言--通用类型栈

    #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <string.h&g ...

  9. 算法系列3《SHA》

    SHA是一种数据加密算法,该算法经过加密专家多年来的发展和改进已日益完善,现在已成为公认的最安全的散列算法之一,并被广泛使用.该算法的思想是接收一段明文,然后以一种不可逆的方式将它转换成一段(通常更小 ...

  10. 记录bigdesk中ElasticSearch的性能参数

    定时采集bigdesk中的Elasticsearch性能参数,并保存到数据库或ELK,以便于进行长期监控. 基于python脚本实现,脚本如下: #coding=gbk import httplibi ...