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. php对函数的引用

    function &example($tmp=0){                  //定义一个函数,别忘了加“&”符     return $tmp;              ...

  2. python大法好——多线程

    Python 多线程 多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的任务放到后台去处理. 用户界面可以更加吸引人,这样比如用户点击了一个按钮去触发某些事件 ...

  3. rest_famework 增删改查初第二阶段(中级,此阶段是优化初级阶段的代码)的使用

    url: re_path('authors/$', views.AuthorView.as_view()),re_path('authors/(\d+)/$', views.AuthorDetailV ...

  4. 38_redux_counter应用_react版本

    redux的核心API 使用非redux创建: 项目结构: index.js import React from 'react'; import ReactDOM from 'react-dom'; ...

  5. POI--各种样式的XSSFCellStyle的生成

    //背景色.フォント色.枠線より各種XSSFCellStyleの作成して.cellStyleMapに保存する private HashMap<String, XSSFCellStyle> ...

  6. JSP 有些类can not be resolved

    看了网上的帖子,切换了jdk到低版本,发现还是不能解决问题. 发现出现问题的代码在tomcat下的Lib包中的其中一个包,jasper.jar 我在想是不是tomcat的版本问题,拷贝了其他地方的ja ...

  7. 把http网站变成https网站

    所需条件: 一个网站域名,必须是注册的合法域名,国内域名最好已经通过备案: 一个空间,可以是虚拟空间或者云主机: 一张SSL证书. 操作步骤: 域名注册.国内的有万网和国外GoDaddy,当然域名注册 ...

  8. 【译】BERT表示的可解释性分析

    目录 从词袋模型到BERT 分析BERT表示 不考虑上下文的方法 考虑语境的方法 结论 本文翻译自Are BERT Features InterBERTible? 从词袋模型到BERT ​ Mikol ...

  9. 安装和破解Navicat

    这里的是安装目录,要记住 额外热任务在桌面创建一个快捷方式 点击完成之后先不要打开运行Navicat.exe这个主程序,把PatchNavicat.exe复制到你刚刚我们安装的路径,,不是你安装包的路 ...

  10. mysql导入太慢解决方法

    半调子数据科学家又要折腾数据,拿到数据一看,3.6G的zip文件,解压看看,卧槽12个G的sql文件.好吧,又要折腾sql数据了.第一件事,肯定是搭一个数据库,导入数据咯. 折腾过sql导入的亲们都知 ...