原题地址

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

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

然后计算出单词之间有几个空格,注意,如果空格总长度无法整除空格数,前面的空格长度通通+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. 关于编程语言(转/收藏)-原文作者:韩天峰(Rango)

    原文在这里:http://rango.swoole.com/archives/405 容易让人记住的文章,要么引起共鸣,要么催人奋进.一句话,你已走过,而我也在路上. 最近群里很多朋友询问我是如何学习 ...

  2. VPN连接失败

    连接VPN是总提示 本来我以为是VPN服务器的问题,可是别人就能连接成功,所以只能是我自己机子的问题.后来检查了一下连接属性,终于发现了问题: 这里“允许使用这些协议”应该是处于选中状态,而我的属性里 ...

  3. xml文件对应的DTD学习

    DTD文件: 1.DTD文档主要由(元素,属性,实体,PCDATA,CDATA) 2.声明一个元素:<!ELEMENT 元素名称 (元素内容)> eg: <!ELEMENT pers ...

  4. [.ashx檔?泛型处理程序?]基础入门#5....ADO.NET 与 将DB里面的二进制图片还原 (范例下载 & 大型控件的ImageField)

    [.ashx檔?泛型处理程序?]基础入门#5....ADO.NET 与 将DB里面的二进制图片还原 (范例下载 & 大型控件的ImageField) http://www.dotblogs.c ...

  5. android 利用TrafficStats类获取本应用的流量

    public void getData() { // PackageManager 包管理类 PackageManager packageManager = BrownserActivity.this ...

  6. Bash美化

    首先声明下,这些美化方式都不是我自己想的,而是多个牛人的方法. 第一:简单点 这个方法来自于:http://www.vimer.cn/?p=1554 没有美化前是这样,鼠标光标在很右边: 在.bash ...

  7. rails中ActionController::InvalidAuthenticityToken解决办法

    Ror代码 class FooController < ApplicationController protect_from_forgery :except => :index # you ...

  8. 菜鸟学习Hibernate——配置Hibernate环境

    一.概念. Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库.既然学习Hibernate那么第 ...

  9. JS对文本框值的判断

    JS判断只能是数字和小数点(摘录自其它资料,在此发表只为个人以后使用查找方便) 1.文本框只能输入数字(不包括小数点) <input onkeyup="this.value=this. ...

  10. Hadoop组成

    Hadoop由以下几个子项目组成: Hadoop Common Hadoop体系最底层的一个模块,为Hadoop各子项目提供各种工具,如:配置文件和日志操作等. Avro Avro是doug cutt ...