题目链接

https://leetcode.com/problems/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 exactly L 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.  "
]

题目大意

输入一个字符串数组和一个规定长度maxWidth。将这个字符串数组的元素尽可能放到长度的L的字符串中,数组中的字符串不能拆开,一个长度L的字符串包括的若干个字符串之间用相等的空格间隔开;如果空格数不能平均分配,则规定左边的空格数可以大于右边的空格数

解题思路

直接模拟解决即可,但需要注意细节的处理,例如从开始加入元素,长度增加,然后空格+1后继续加入新元素,直到长度大于maxWidth

代码

class Solution(object):def fullJustify(self, words, maxWidth):""":type words: List[str]:type maxWidth: int:rtype: List[str]"""ans = []i = 0while i < len(words):size = 0begin = iwhile i < len(words):if size == 0:newsize = len(words[i])else:newsize = size + len(words[i]) + 1if newsize <= maxWidth:size = newsizeelse:breaki += 1spaceCnt = maxWidth - sizeif i - begin - 1 > 0 and i < len(words):everyCount = spaceCnt // (i - begin - 1)spaceCnt %= i - begin - 1else:everyCount = 0j = begin;s=""while j < i:if j == begin:s = words[j]else:s += ' ' * (everyCount + 1)if spaceCnt > 0 and i < len(words):s += ' 'spaceCnt -= 1s += words[j]j += 1s += ' ' * spaceCntans.append(s)return ans

转载于:https://www.cnblogs.com/slurm/p/5124905.html

[Leetcode]@python 68. Text Justification相关推荐

  1. LeetCode 68. Text Justification

    LeetCode 68. Text Justification Solution1:我的答案 除了写的比较慢,没有其他很大的问题.. class Solution { public:vector< ...

  2. LeetCode | Text Justification

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

  3. Python - Sublime Text 3 控制台不能输出中文的解决方法

    Python - Sublime Text 3 控制台不能输出中文的解决方法 参考文章: (1)Python - Sublime Text 3 控制台不能输出中文的解决方法 (2)https://ww ...

  4. python读取文件某一行-使用python读取.text文件特定行的数据方法

    如何用python循环读取下面.txt文件中,用红括号标出来的数据呢? 首先,观察数据可知,不同行的第一个数据元素不一样,所以考虑直接用正则表达式. 再加上,对读和写文件的操作,就行了 注:我用的是p ...

  5. python读取文件多行内容-使用python读取.text文件特定行的数据方法

    如何用python循环读取下面.txt文件中,用红括号标出来的数据呢? 首先,观察数据可知,不同行的第一个数据元素不一样,所以考虑直接用正则表达式. 再加上,对读和写文件的操作,就行了 注:我用的是p ...

  6. python Tkinter Text的简单用法

    1.设置python Tkinter Text控件文本的方法 text.insert(index,string)  index = x.y的形式,x表示行,y表示列 向第一行插入数据,text.ins ...

  7. Leetcode python《热题 HOT 100》15. 三数之和

    Leetcode python 之 <热题 HOT 100>:https://leetcode-cn.com/problemset/hot-100/ 15. 三数之和 给定一个包含 n 个 ...

  8. Leetcode python《热题 HOT 100》1. 两数之和

    Leetcode python 之 <热题 HOT 100>:https://leetcode-cn.com/problemset/hot-100/ 1. 两数之和 给定一个整数数组 nu ...

  9. Python中Text函数常用增删改查阅用法实例汇总

    本篇文章:主要讲解python的Text函数的常见用法,说明了常见用法的相关实例教程 日期:2022年1月27日 Text函数的显示 以下参数和信息为部分Text的函数参数,汇总的一些常用的参数和实例 ...

最新文章

  1. opencv利用直方图判断人脸光照质量
  2. qt 运行库 linux,linux(ubuntu) 版qt5.x安装的一些知识
  3. 使用git了解代码编写过程
  4. HEVC/H265 HM10.0 分析(三)TAppDecTop.cpp
  5. 「雕爷学编程」Arduino动手做(18)---太阳能电池模块
  6. html游戏图标库,又一款开源图标库 CSS.GG,值得一用
  7. (42)FPGA状态机二段式
  8. weixintong ent.php,多商户版微信商城对接微信公众号的方法及设置
  9. 设置SVN忽略文件和文件夹(文件夹)
  10. cemtos7.2搭建samba
  11. 引用css样式时,一个class引用两个样式的写法( class=ico001 icoCom)。
  12. 实战自制Linux操作系统
  13. 30岁菜鸟涛学习VB.net 第十三天
  14. win7硬盘安装工具_扔掉U盘和光驱,一键从硬盘安装操作系统,这个硬盘装机工具真爽...
  15. 计算机科学型,函数计算器1005B学生专用 多功能计算机科学函数型
  16. matlab hist3 密度图,Matlab中hist3
  17. 数组的缩减(简易版)
  18. Android 检索相册视频文件
  19. Python代码大全,海量代码任你下载
  20. java scavenge_Java垃圾收集器之Parallel Scavenge收集器

热门文章

  1. Mysql 5.7.10以上版本安装大坑
  2. 解决Eclipse无法打开“Failed to load the JNI shared library”(转)
  3. Java nio Socket非阻塞模式
  4. 阿里面试题——Java对象初始化
  5. MySQL和PostgreSQL的常用语法差异
  6. tesseract-ocr tips
  7. 【NetApp】NetBoot的使用方法
  8. location.search
  9. 洛谷 P2513 [HAOI2009]逆序对数列
  10. bzoj 4446: [Scoi2015]小凸玩密室