[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 ...
随机推荐
- ubuntu 装机步骤表
步骤 1. root 步骤 apt-get update ; apt-get upgrate apt-get install git zsh apt-get install -y make build ...
- Java Web组件Servlet、Filter、Listener
一.Servlet 类javax.servlet.http.HttpServlet; 1.是单例模式,一个web容器中只有一个实例.服务器调用它的service方法进行请求处理, service方法又 ...
- Garbage-Only-One的IO路
我的任务计划 刷题计划 BZOJ 1.1202 2.1008 3.等等 搜索 1.搜索题单 学习计划 树 1.树状数组or树状数组or树状数组 2.线段树 搜索 1.A*
- Kafka connect in practice(3): distributed mode mysql binlog ->kafka->hive
In the previous post Kafka connect in practice(1): standalone, I have introduced about the basics of ...
- FTP学习笔记
FTP有两个连接方式 1.控制连接 2.数据连接 控制链接 标准端口为21 用于数据传输中的控制 数据连接 标准端口20 用于数据传输中的上传 下载数据 数据传输的连接方式,主动连接 被动连接. ...
- 初识git(17/8/21)
git是一个分布式的版本管理系统 通过廖雪峰的官方网站(maybe2017)来学习的,比较详实跟着操作就行,记录基本的一些命令还有学习是遇到的一些问题和收获,方便下次查阅. git的安装 -. win ...
- 查询最新记录的sql语句效率对比
在工作中,我们经常需要检索出最新条数据,能够实现该功能的sql语句很多,下面列举三个进行效率对比 本次实验的数据表中有55万条数据,以myql为例: 方式1: SELECT * FROM t_devi ...
- rest_famework 认证与权限组件
定义个一个认证类 from rest_framework import exceptionsfrom rest_framework.authentication import BaseAuthenti ...
- ImportError: No module named managers
代码: import os import cPickle as pickle filename = '../dftest.pkl' if(os.path.exists(filename)): w=op ...
- 微信小程序商城开源项目,Weixin-App-Shop 1.0 版本正式发布!!!
微信小程序商城开源项目,Weixin-App-Shop 1.0 版本正式发布 Weixin-App-Shop 是捷微团队开发的微信小程序商城开源项目,涵盖了微信商城的全部功能,能够快速发布简单易用的小 ...