leetcode Text Justification

leetcode Text Justification

Given an array of words and a length L, format the text such that each line has exactly L 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 exactlyL 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.

For example,
words: ["This", "is", "an", "example", "of", "text", "justification."]
L: 16.

Return the formatted lines as:

[
“This is an”,
“example of text”,
“justification. ”
]

Note: Each word is guaranteed not to exceed L in length.

 

题目地址:leetcode Text Justification

题意:给定n个单词,要求将这n个单词按照如下规则打印出来。

  1. 每一行长度为L。
  2. 单词与单词之间用空格隔开,如果总长度不够L,则补充额外的空格。单词与单纯之间空格数应尽可能相等。
  3. 最后一行单词与单词之间不需要补充额外的空格,但是长度也要L

思路:按照题目要求来呗。。。注意细节。

 

C++

 

Python

 

本博客若无特殊说明则由 hrwhisper 原创发布
转载请点名出处:细语呢喃 > leetcode Text Justification
本文地址:https://www.hrwhisper.me/leetcode-text-justification/

打赏一杯咖啡钱呗

codes, Leetcode , , , . permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *