题目链接

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. 【AdaBoost算法】基于OpenCV实现人脸检测Demo

    一.关于检测算法 分类器训练: 通过正样本与负样本训练可得到分类器,opencv有编译好的训练Demo,按要求训练即可生成,这里我们直接使用其已经训练好的分类器检测: 检测过程: 检测过程很简单,可以 ...

  2. Python 虚拟环境:Virtualenv

    安装sudo yum install python-virtualenv 使用方法 virtualenv [虚拟环境名称] 如,创建**ENV**的虚拟环境 virtualenv ENV 默认情况下, ...

  3. 《ASP.NET MVC 5 框架揭秘》

    <ASP.NET MVC 5 框架揭秘> 基本信息 作者: 蒋金楠 出版社:电子工业出版社 ISBN:9787121237812 上架时间:2014-8-1 出版日期:2014 年8月 开 ...

  4. hibernate集合类型映射

    Set无序 元素不可重复 List有序 元素可重复 Bag无序 元素可重复 Map键值对 Student: package model; import java.util.Set; public cl ...

  5. Python开发之【简单计算器】

    开发一个简单的python计算器 1.实现加减乘除及括号优先级解析 2.用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * ...

  6. xamarin.android之 Android 4.4+ 获取图片真实路径

    Android 4.4以下 选择图片是可以获取到图片路径的.高于Android 4.4获取图片路径只是获取到一个图片编号. 所以需要针对Android版本进行路径解析: #region 高于 v4.4 ...

  7. Android之TelephonyManager类的方法详解

    TelephonyManager类主要提供了一系列用于访问与手机通讯相关的状态和信息的get方法.其中包括手机SIM的状态和信息.电信网络的状态及手机用户的信息.在应用程序中可以使用这些get方法获取 ...

  8. jQuery选择器简单例子

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="jQuery_5.aspx. ...

  9. char,vchar,nchar,nvchar的区别

    char和varchar的长度都在1到8000之间,它们的区别在于char是定长字符数据,而varchar是变长字符数据.所谓定长就是长度固定的,当输入的数据长度没有达到指定的长度时将自动以英文空格在 ...

  10. 译文 对无障碍网页应用(ARIA)的选择

    //本文编辑格式为Markdown,译文同时发布在众成翻译 对无障碍网页应用(ARIA)的选择 让网站对每个人都能访问是一件相当艰难的工作,尤其是在我们使用自定义标记解决方案(custom marku ...