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. C++ 虚函数实例

    #include <iostream> using namespace std; //线 class Line { public: Line(float len); ; ; protect ...

  2. Linux命令之---nl

    命令简介 nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文件内容自动的加上行号!其默认的结果与 cat -n 有点不太一样, nl 可以将行号做比较多的显示设计,包括位数与是否自动补 ...

  3. Redis实现之对象(四)

    类型检查与命令多态 Redis中用于操作键的命令基本上可以分为两种类型:其中一种命令可以对任何类型的键执行,比如DEL命令.EXPIRE命令.RENAME命令.TYPE命令.OBJECT命令等.举个栗 ...

  4. 2019腾讯暑期实习面试(offer)前端

    最近在忙着准备找实习,所以没有更新之前的文章. 不过所幸功夫不负有心人,我拿到了腾讯的offer. 这里分享一下面试的经验. 简介 本人双非本科,普通学生一枚. 面的是腾讯的Web前端开发. 整个面试 ...

  5. 使用 Dom4j 将 XML 转换为 MAP

    本文为转载:http://blog.sina.com.cn/s/blog_6145ed810100z164.html  原文地址. 自己仅作备忘录方便查找留了一份. 这是解析Xml 的辅助类 pack ...

  6. 矩阵儿快速幂 - POJ 3233 矩阵力量系列

    不要管上面的标题的bug 那是幂的意思,不是力量... POJ 3233 Matrix Power Series 描述 Given a n × n matrix A and a positive in ...

  7. mac攻略(九) -- ssh工具secureCRT

    mac ssh 客户端 : 本身mac直接使用终端来ssh连接就很方便,但是使用过程中随着远程服务器的增多和zsh和远程服务器编码不同产生了乱码,决定安装一款ssh终端软件,以下方法亲测可用,感谢提供 ...

  8. 分布式存储系统可靠性系列五:副本放置算法 & CopySet Replication

    本文来自网易云社区 作者:孙建良 在分布式存储系统 中说明了,在一定情况下,copyset的数量不是越多越好,在恢复时间确定的情况下,找到合适的copyset的数量可以降低数据丢失的概率. 在分布式存 ...

  9. MFC深入浅出读书笔记第二部分2

    第七章  MFC骨干程序 所谓骨干程序就是指有AppWizard生成的MFC程序.如下图的层次关系是程序中常用的几个类,一定要熟记于心. 1 Document/View应用程序 CDocument存放 ...

  10. MongoDB快速入门学习笔记2 MongoDB的概念及简单操作

    1.以下列举普通的关系型数据库和MongoDB数据库简单概念上的区别: 关系型数据库 MongoDB数据库 说明 database database 数据库 table collection 数据库表 ...