[leetcode]68. Text Justification文字对齐
Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth 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 maxWidth characters.
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.
Note:
- A word is defined as a character sequence consisting of non-space characters only.
- Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
- The input array
wordscontains at least one word.
Example 1:
Input:
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
Output:
[
"This is an",
"example of text",
"justification. "
]
Example 2:
Input:
words = ["What","must","be","acknowledgment","shall","be"]
maxWidth = 16
Output:
[
"What must be",
"acknowledgment ",
"shall be "
]
Explanation: Note that the last line is "shall be " instead of "shall be",
because the last line must be left-justified instead of fully-justified.
Note that the second line is also left-justified becase it contains only one word.
Example 3:
Input:
words = ["Science","is","what","we","understand","well","enough","to","explain",
"to","a","computer.","Art","is","everything","else","we","do"]
maxWidth = 20
Output:
[
"Science is what we",
"understand well",
"enough to explain to",
"a computer. Art is",
"everything else we",
"do "
]
题意
给定一列单词,按照指定宽度将其排成工整格式。
code
[leetcode]68. Text Justification文字对齐的更多相关文章
- [LeetCode] 68. Text Justification 文本对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- leetcode@ [68] Text Justification (String Manipulation)
https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...
- Leetcode#68 Text Justification
原题地址 没有复杂的算法,纯粹的模拟题 先试探,计算出一行能放几个单词 然后计算出单词之间有几个空格,注意,如果空格总长度无法整除空格数,前面的空格长度通通+1 最后放单词.放空格,组成一行,加入结果 ...
- 【一天一道LeetCode】#68. Text Justification
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [Leetcode] text justification 文本对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- 【LeetCode】68. Text Justification
Text Justification Given an array of words and a length L, format the text such that each line has e ...
- 68. Text Justification一行单词 两端对齐
[抄题]: Given an array of words and a width maxWidth, format the text such that each line has exactly ...
- 【leetcode】Text Justification
Text Justification Given an array of words and a length L, format the text such that each line has e ...
- 68. Text Justification
题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...
随机推荐
- c++字节对齐
参考URL: http://blog.csdn.net/hairetz/article/details/4084088 0 字节对齐的意义按我的理解是便于cpu一次取完所有数据, 提高代码的执行效 1 ...
- .net core2.2
GetCurrentDirectory returns the worker directory of the process started by IIS rather than the app's ...
- New Journey--工作五年所思所感小记
正式参加工作满五年了,2013年7月9日走入游戏行业.之前就计划着应该提笔写点什么留下来,但当日子真的到了,竟是不知说些什么了.14年年中的时候我曾写过一篇工作一年的总结,现在回头读起来,还真是别有一 ...
- 关于getClass()的一些自己的见解
通过观看视频讲解,我得知,Class 老师讲解的是 这个是类的类,即是管理类的类.当时不是很理解,随后查询了相关资料后,以及真切的运行了几行代码后,才有一些深层次的体会, 在这里想要跟大家分享一下.如 ...
- React高级指引
深入JSX 本质上来讲,JSX是为React.createElement方法提供的语法糖 <MyButton color=}> Click Me </MyButton> 编译为 ...
- leetCode66:加一
/** * @param {number[]} digits * @return {number[]} */ var plusOne = function(digits) { if(digits[di ...
- (21/24) webpack实战技巧:webpack对三方类库的优化操作
1.优化第一步--选用引入方式 在前面的学习中我们对于如何引入第三方类库(Jquery)推荐了两种方式,第一种是import引入的方式,第二种是ProvidePlugin插件.那两种引入方法有什么区别 ...
- supergridcontrol记录,分页
sqlserver分页记录 select top 50 DengJiBH,sSuoYouQuanShenQingRen,sZuoLuo,sQiuHao,sQuanHao,ChaXun_BianHao, ...
- Java中使用MongoDB
一.为MongoDB设置用户名和密码 MongoDB中验证用户和密码的条件: 1. 服务器启动的时候打开授权认证,即修改MongoDB的配置文件mongodb.conf,增auth = true # ...
- linux7 安装 zlib依赖库 与安装python 3.6
Linux 安装zlib依赖库 进入src: cd /usr/local/src 下载zlib库: wget http://www.zlib.net/zlib-1.2.11.tar.gz 解压下载的t ...