题目链接

Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.

You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly Lcharacters.

Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.

For the last line of text, it should be left justified and no extra space is inserted between words.

For example,
words: ["This", "is", "an", "example", "of", "text", "justification."]
L: 16.

Return the formatted lines as:

[
"This is an",
"example of text",
"justification. "
]

Note: Each word is guaranteed not to exceed L in length.

Corner Cases:

  • A line other than the last line might contain only one word. What should you do in this case?
    In this case, that line should be left-justified.

分析:这一题需要注意两个点,a、当该行只放一个单词时,空格全部在右边 b、最后一行中单词间只有一个空格,其余空格全部在右边。然后只要贪心选择,在一行中尽量放多的单词。                                                                                             本文地址

class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vector<string>res;
int len = words.size(), i = 0;
while(i < len)
{
//if(words[i].size() == 0){i++; continue;}
int rowlen = 0, j = i;
while(j < len && rowlen + words[j].size() <= L)
rowlen += (words[j++].size() + 1);
//j-i是该行放入单词的数目
if(j - i == 1)
{//处理放入一个单词的特殊情况
res.push_back(words[i]);
addSpace(res.back(), L - words[i].size());
i = j; continue;
}
//charaLen是当前行字母总长度
int charaLen = rowlen - (j - i);
//平均每个单词后的空格,j < len 表示不是最后一行
int meanSpace = j < len ? (L - charaLen) / (j - i - 1) : 1;
//多余的空格
int leftSpace = j < len ? (L - charaLen) % (j - i - 1) : L - charaLen - (j - i -1);
string tmp;
for(int k = i; k < j - 1; k++)
{
tmp += words[k];
addSpace(tmp, meanSpace);
if(j < len && leftSpace > 0)
{
tmp.push_back(' ');
leftSpace--;
}
}
tmp += words[j - 1];//放入最后一个单词
if(leftSpace > 0)addSpace(tmp, leftSpace); //对最后一行
res.push_back(tmp);
i = j;
}
return res;
} void addSpace(string &s, int count)
{
for(int i = 1; i <= count; i++)
s.push_back(' ');
}
};

【版权声明】转载请注明出处http://www.cnblogs.com/TenosDoIt/p/3475275.html

LeetCode:Text Justification的更多相关文章

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

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

  2. [leetcode]Text Justification @ Python

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

  3. LeetCode: Text Justification 解题报告

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

  4. [Leetcode] text justification 文本对齐

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

  5. [LeetCode] Text Justification words显示的排序控制

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

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

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

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

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

  8. LeetCode OJ——Text Justification

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

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

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

随机推荐

  1. Effective Java 18 Prefer interfaces to abstract classes

    Feature Interface Abstract class Defining a type that permits multiple implementations Y Y Permitted ...

  2. JavaScript Patterns 4.1 Functions Background

    Functions are first-class objects and they provide scope. • Can be created dynamically at runtime, d ...

  3. Tesseract-OCR 字符识别---样本训练 [转]

    Tesseract是一个开源的OCR(Optical Character Recognition,光学字符识别)引擎,可以识别多种格式的图像文件并将其转换成文本,目前已支持60多种语言(包括中文).  ...

  4. c#,关于Big Endian 和 Little Endian,以及转换类

    Big Endian:最高字节在地址最低位,最低字节在地址最高位,依次排列. Little Endian:最低字节在最低位,最高字节在最高位,反序排列. 当在本地主机上,无需注意机器用的是Big En ...

  5. oracle的增删改查语句

    创建一个表: cteate table 表名(列1 类型, 列2 类型);查看表结构 desc表名添加一个字段 alter table 表名 add(列类型);修改字段类型 alter table 表 ...

  6. 在IT的路上,我在成长

    在IT的路上,我在成长.很荣幸地加入了博客园这个大家庭. 岁月的航船在不断航行,在成长的脚印我要深深留下,回首已往经历,发现自己成长的路上,将来也会有很多美好的回忆,以及丰硕的果实.

  7. Tomcat 内存和线程配置优化

    1. tomcat 的线程配置参数详情如下: 修改conf/server.xml中的<Connector .../> 节点如下: <Connector port="8080 ...

  8. c++ initialize_list

    看到这么一个东西,可以实现花括号( "{" "}" )初始化容器类. 使用时需包含头文件 #include <initialize_list> 我们 ...

  9. 《JavaScript修炼之道》读书笔记

    1.参考书目 入门:<JavaScript DOM编程艺术>第二版 进阶:<JavaScript高级程序设计>第二版.<JavaScript编程精粹> <Ja ...

  10. Quicksum-S.B.S.

    quicksum Queation: Given a string of digits, find the minimum number of additions required for the s ...