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.

click to show corner cases.

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.

首先要做的就是确定每一行能放下的单词数,这个不难,就是比较n个单词的长度和加上n - 1个空格的长度跟给定的长度L来比较即可,找到了一行能放下的单词个数,然后计算出这一行存在的空格的个数,是用给定的长度L减去这一行所有单词的长度和。得到了空格的个数之后,就要在每个单词后面插入这些空格,这里有两种情况,比如某一行有两个单词"to" 和 "a",给定长度L为6,如果这行不是最后一行,那么应该输出"to   a",如果是最后一行,则应该输出 "to a  ",所以这里需要分情况讨论,最后一行的处理方法和其他行之间略有不同。最后一个难点就是,如果一行有三个单词,这时候中间有两个空,如果空格数不是2的倍数,那么左边的空间里要比右边的空间里多加入一个空格,那么我们只需要用总的空格数除以空间个数,能除尽最好,说明能平均分配,除不尽的话就多加个空格放在左边的空间里,以此类推,

class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L){
vector<string> result;
// input validation
if(words.empty()) return result; // param @ i 遍历单词的下标临时变量
// param @ k 校正后每行单词个数
// param @ l 每行字符(除空格)个数
// 每次遍历构造一行 将单词检索进新数组
for(int i = , k, l; i < words.size(); i += k){
// 查询单词不能越界 并且每行增添的单词需要满足要满足中间能插入空格
//
for(k = , l = ; k+i < words.size() && l + words[i+k].size()<= L-k;
l += words[i+k].size(), ++k); // 构造新行
string temp = words[i]; // 每行首单词
for(int j = ; j < k-; ++j){
int blanks = (L-l)/(k-) + (j < (L-l)%(k-));
// 已经是最后一行单词,要求左对齐,每个单词之间一个空格,剩下空格放在最后
if(i+k >= words.size()){
temp += " ";
}
else temp += string(blanks, ' ');
temp += words[i+j+];
}
// 补全空格
temp += string(L-temp.size(), ' ');
result.push_back(temp);
}
return result;
}
};

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. Text Justification,文本对齐

    问题描述:把一个集合的单词按照每行L个字符放,每行要两端对齐,如果空格不能均匀分布在所有间隔中,那么左边的空格要多于右边的空格,最后一行靠左对齐. words: ["This", ...

  3. [Leetcode] text justification 文本对齐

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

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

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

  5. Text Justification 实现两端对齐功能

    实现office word中的两端对齐功能. 只有个单词时,右边补齐空格.最后一行每个词间一个空格,整下的空格右边补齐.给定字符串,和每行的字符数L.进行两端对齐输出. 我的思路是写一个函数,给定相应 ...

  6. [Swift]LeetCode68. 文本左右对齐 | Text Justification

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

  7. [MIT6.006] 20. Daynamic Programming II: Text Justification, Blackjack 动态规划II:文本对齐,黑杰克

    这节课通过讲解动态规划在文本对齐(Text Justification)和黑杰克(Blackjack)上的求解过程,来帮助我们理解动态规划的通用求解的五个步骤: 动态规划求解的五个"简单&q ...

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

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

  9. 68. Text Justification一行单词 两端对齐

    [抄题]: Given an array of words and a width maxWidth, format the text such that each line has exactly  ...

随机推荐

  1. Objective-C 入门笔记

    简介 建立在C语言之上,可以混编C/C++代码,编写一个类需要二个文件: .h的头文件 .m的实现文件(如果是C/C++混编文件,文件后缀为.mm) 既然有头文件,所以很多人会拿它与C++进行类比,它 ...

  2. AppStore中使用IDFA后提交应用的注意事项

    在ios7.0出来以前,我们都是通过wifi的mac来当作IOS设备的唯一标识符.如何在ios下获取设备的MAC,你可以参数这篇文章:获取ios的MAC地址 在没有使用IDFA之前,我们在ios7及以 ...

  3. 关于Java中的equals方法

    关于Java中的equals方法 欢迎转载,但是请填写本人的博客园原址https://www.cnblogs.com/JNovice/p/9347099.html 一.什么是equals方法 equa ...

  4. 开启VIM的Python支持

    开启VIM的Python支持 2015年01月03日 02:57:58 forlong401 阅读数:16294更多 个人分类: VIPython   http://www.tuicool.com/a ...

  5. HTML5 浏览器返回按钮/手机返回按钮事件监听

    1.HTML5  History对象 支持使用pushState()方法修改地址栏地址,而不刷新页面. popstate事件 当history实体被改变时,popstate事件将会发生.调用pushS ...

  6. [填坑]解决"Your MaintenanceTool appears to be older than 3.0.2. ."问题

    之前我写过QT5.9版本在更新组件时出现“要继续此操作,至少需要一个有效且已启用的储存库”问题,得到了网友的热心转载,说明遇到此问题的人不在少数. 原文地址:https://blog.csdn.net ...

  7. C++中一些类和数据结构的大小的总结

    针对class,虚函数等情况写了一些代码测试. #include <stdio.h> class A { }; class B { public: void f(); void g(); ...

  8. [leetcode]Rotate Image @ Python

    原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: You are given an n x n 2D matrix representin ...

  9. Commands to help you to Start Using ScaleIO Storage

    To start using your storage: Log in to the MDM: scli --login --username admin --password <passwor ...

  10. centos:时间同步

    转自:https://blog.csdn.net/u011391839/article/details/62892020 Linux的时间分为System Clock(系统时间)和Real Time ...