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 words contains 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文字对齐的更多相关文章

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

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

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

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

  3. Leetcode#68 Text Justification

    原题地址 没有复杂的算法,纯粹的模拟题 先试探,计算出一行能放几个单词 然后计算出单词之间有几个空格,注意,如果空格总长度无法整除空格数,前面的空格长度通通+1 最后放单词.放空格,组成一行,加入结果 ...

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

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

  5. [Leetcode] text justification 文本对齐

    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

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

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

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

  8. 【leetcode】Text Justification

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

  9. 68. Text Justification

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

随机推荐

  1. 2、Netty基础

    一.前言 主要包含下面内容: 初识 Netty: 使用 Java NIO 搭建简单的客户端与服务端实现网络通讯: 使用 Netty 搭建简单的客户端与服务端实现网络通讯: Netty 底层操作与 Ja ...

  2. 支持向量机(SVM)原理阐述

    支持向量机(Support Vector Machine, SVM)是一种二分类模型.给定训练集D = {(x1,y1), (x2,y2), ..., (xm,ym)},分类学习的最基本的想法即是找到 ...

  3. spark2.1源码分析2:从SparkPi分析一个job的执行

    从SparkPi的一个行动操作入手,选择Run–Debug SparkPi进入调试: F8:Step Over F7:Step Into 右键Run to Cursor Ctrl+B 查看定义 导航– ...

  4. 转发:VB程序操作word表格(文字、图片)

    很多人都知道,用vb操作excel的表格非常简单,但是偏偏项目中碰到了VB操作word表格的部分,google.baidu搜爆了,都没有找到我需要的东西.到是搜索到了很多问这个问题的记录.没办法,索性 ...

  5. C语言编程漫谈——main函数

    写在前面 促使我写这篇文章是因为我这几天找了几个一样是大三的同学,与我相同专业相同方向(物联网)的人,除了@小胡同的诗,基本没有什么其他人会现在看起来很简单的编程题目了.问了一下其他同学,他们大部分都 ...

  6. OpenCV中 常用 函数 的作用

    1.CV_Assert函数作用: CV_Assert()若括号中的表达式值为false,则返回一个错误信息.

  7. js实现输入某串数字,构建完全二叉树,并判断是否为二叉搜索树

    思路:若为二叉搜索树,则中序遍历为递增的 let arr = [15,8,16,6,10];let pindex = [];function Node(){ this.root = null; thi ...

  8. Nginx 负载配置

    简版的,详细参数需要自己微调. nginx.conf http{ upstream name { server 127.0.0.1:8777; server 127.0.0.1:8778; serve ...

  9. run in thread

    def run_in_thread(runnable, is_daemon=True): server_thread = Thread(target=runnable) server_thread.s ...

  10. Oracle树形查询

    sql树形递归查询是数据库查询的一种特殊情形,也是组织结构.行政区划查询的一种最常用的的情形之一.下面对该种查询进行一些总结: start with子句: 递归的条件,需要注意的是如果with后面的值 ...