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. C++primer第一章(部分)

    1.4.2 for 语句 for (init-statement; condition; expression) statement; step1:初始化 step2:判断条件,为真则执行循体:为假则 ...

  2. mysql5.7基于gtid的主从复制

    两个节点:mkdir -p /data/mysql-5721/datamkdir -p /data/mysql-5721/innodbmkdir -p /data/mysql-5721/replica ...

  3. ubuntu16.04下latex环境搭建

    背景: 最近需要使用latex做一下简历~~~ 工具: sublime text3 (plugin: latextools) + texlive-full 配置: Preferences -> ...

  4. 使用阿里云Java SDK 实现 DDNS

    本代码的实现前提: 1.拥有阿里云域名,且获取了Access Key 及 Access Secret 2.能获取外网IP的页面地址(注意:ip138.com的实际包含ip地址为http://2018. ...

  5. 初识Velocity

    哇,好长时间没有写文章啦~ 楼主最近在工作中认识了一个叫做Velocity的java的模板引擎,小白的我去网上看了一下,应用还蛮多的,然而我目前接触到的只是用于基于模板生成这块的知识,想写个文章记下, ...

  6. opencv人脸检测,旋转处理

    年会签到,拍自己的大头照,有的人可能会拍成横向的,需要旋转,用人脸检测并修正它(图片). 1. 无脑检测步骤为: 1. opencv 读取图片,灰度转换 2. 使用CascadeClassifier( ...

  7. jinja2 把文本变成html

    data 是传过去的值 过滤器 | {{data.name | safe}}

  8. 深入理解Java虚拟机读书笔记2----垃圾收集器与内存分配策略

    二 垃圾收集器与内存分配策略 1 JVM中哪些内存需要回收?     JVM垃圾回收主要关注的是Java堆和方法区这两个区域:而程序计数器.虚拟机栈.本地方法栈这3个区域随线程而生,随线程而灭,随着方 ...

  9. 利用EventHandler系统委托,触发Event

                   最近一直在研究委托与事件,下面是我的个人理解 NOTE:参考了张子阳的思想,各位大佬可以看看http://www.cnblogs.com/JimmyZhang/archi ...

  10. JAX-RS和 Spring 整合开发

    JAX-RS 和 和 Spring 整合开发 1.建立maven项目 2.导入maven坐标 <dependencies> <!-- cxf 进行rs开发 必须导入 --> & ...