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;
}
};

LeetCode OJ-- Text Justification的更多相关文章

  1. LeetCode OJ——Text Justification

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

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

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

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

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

  4. 【leetcode】Text Justification

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

  5. 【leetcode】Text Justification(hard) ☆

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

  6. Java for LeetCode 068 Text Justification

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

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

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

  8. Leetcode#68 Text Justification

    原题地址 没有复杂的算法,纯粹的模拟题 先试探,计算出一行能放几个单词 然后计算出单词之间有几个空格,注意,如果空格总长度无法整除空格数,前面的空格长度通通+1 最后放单词.放空格,组成一行,加入结果 ...

  9. [LeetCode] Text Justification 文本左右对齐

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

  10. [leetcode]Text Justification @ Python

    原题地址:https://oj.leetcode.com/problems/text-justification/ 题意: Given an array of words and a length L ...

随机推荐

  1. Matlab数据转化至python端,并写入数据库

    因工作原因,一些获取的行业数据以已知的结构体存储在.mat文件中, 现需要将其存储在数据库中并且能够灵活调用至python dataframe里进行操作 原数据的一个例子如下 目标如上: 然后是转化代 ...

  2. exec , 元类,__new__, __call__ , 单例模式 , 异常

    1,类也是对象 ''' 动态语言 可以在运行期间 动态生成类 修改对象属性 静态语言 ''''' ''' type(object_or_name, bases, dict) type(object) ...

  3. day09-函数讲解

    1.如何定义一个函数 s = '华为加油a' def s_len(): i = 0 for k in s: i += 1 print(i) s_len() 这个函数的功能就是输出字符串的长度.但是他只 ...

  4. 动态规划:HDU1003-Max Sum(最大子序列和)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  5. 借助FreeHttp为任意移动端web网页添加vConsole调试

        以下介绍在不用修改代码并发布项目的情况下,为我们日常使用的移动web应用(如手机web淘宝)添加vConsole调试工具的方法   vConsole介绍 vConsole是一个轻量.可拓展.针 ...

  6. cf979d Kuro and GCD and XOR and SUM

    set做法 正解是trie-- 主要是要学会 \(a\ \mathrm{xor}\ b \leq a+b\) 这种操作 #include <iostream> #include <c ...

  7. C#操作XML配置文件

    代码为C#操作xml配置文件的范例类,函数SetValue用于向配置文件写入一个值,GetValue用于根据Key获取相应值。这种方法的配置文件不需要手动创建,程序在运行后会自动处理创建。 注意:1. ...

  8. 12、jQuery知识总结-2

    1.避免冲突 jQuery 使用 $ 符号作为 jQuery 的简介方式 <html> <head> <script type="text/javascript ...

  9. selenium定位下拉菜单好文

    http://www.cnblogs.com/nbkhic/archive/2011/10/23/2221726.html 定位下拉菜单

  10. activemq概念介绍

    ActiveMQ概念介绍 是Apache下的开源项目,完全支持JMS1.1和J2EE1.4规范的JMS Provider实现,消息中间件. 消息中间件: A传递消息到B(功能或者系统),有比较强的耦合 ...