【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 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. "
]
Note: Each word is guaranteed not to exceed L in length.
题解:题目倒是不难,按照题目一步步走就是了,就是细节太多了,交了好多次T_T
从位置i开始往前取词,直到取到的词长度大于L停止取词,假设此时取到第j个词,用curLen统计这些词和它们之间的空格的长度,那么剩下来要额外平均分配到各个单词之间的空格数目就是totalSpaces = L-curLen,所以每个单词之间平均分配(L-curLen)/(j-i)个空格,但是有可能L-curLen不能被(j-i)除尽,那么余出来的空格又要一个一个插入到第一个单词间隙,第二个单词间隙......(注意不是全部插入到第一个单词间隙中)。对于最后一行,将totalSpaces直接设置为0,即不添加额外的空格,然后在结尾处填充空格使其长度达到L。
代码如下:
 public class Solution {
     private String genericSpaces(int num){
         StringBuffer sb = new StringBuffer();
         for(int i = 0;i < num;i++)
             sb.append(" ");
         return sb.toString();
     }
     public List<String> fullJustify(String[] words, int L) {
         List<String> answer = new ArrayList<String>();
         int curLen = 0;
         for(int i = 0;i < words.length;){
             curLen = 0;
             int j = i;
             for(;j<words.length && curLen+words[j].length()<=L;){
                 curLen = curLen + words[j].length() + 1;
                 j++;
             }
             curLen--;
             int totalSpace = L-curLen;
             j--;
             int eachSpace = j==i?0:totalSpace/(j-i);
             //handle last line, there are only one spaces bewteen words
             if(j == words.length-1){
                 totalSpace = 0;
                 eachSpace = 0;
             }
             StringBuffer concate = new StringBuffer();
             for(int k = i;k<=j;k++){
                 concate.append(words[k]);
                 if(k != j)
                     concate.append(" ");
                 String spaces;
                 //if totalSpace can't be evenly distributed
                 if(totalSpace-eachSpace*(j-i)!=0){
                     spaces = genericSpaces(eachSpace+1);
                     totalSpace--;
                 }
                 else {
                     spaces = genericSpaces(eachSpace);
                 }
                 //last word isn't followed by spaces
                 if(k != j)
                     concate.append(spaces);
             }
             //to handle the last line, adding extra spaces to reach length L
             while(concate.length()<L)
                 concate.append(" ");
             answer.add(concate.toString());
             i=j+1;
         }
         return answer;
     }
 }
【leetcode刷题笔记】Text Justification的更多相关文章
- LeetCode刷题笔记和想法(C++)
		
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
 - 18.9.10 LeetCode刷题笔记
		
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
 - LeetCode刷题笔记 - 12. 整数转罗马数字
		
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
 - Leetcode刷题笔记(双指针)
		
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
 - LeetCode刷题笔记(1-9)
		
LeetCode1-9 本文更多是作为一个习题笔记,没有太多讲解 1.两数之和 题目请点击链接 ↑ 最先想到暴力解法,直接双循环,但是这样复杂度为n平方 public int[] twoSum(int ...
 - leetcode刷题笔记
		
(1)Best Time to Buy and Sell Stock Total Accepted: 10430 Total Submissions: 33800My Submissions Say ...
 - leetcode刷题笔记08 字符串转整数 (atoi)
		
题目描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即 ...
 - LeetCode刷题笔记-回溯法-分割回文串
		
题目描述: 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab"输出:[ ["aa", ...
 - leetcode刷题笔记231 2的幂
		
题目描述: 给定一个整数,写一个函数来判断它是否是2的幂. 题目分析: 判断一个整数是不是2的幂,可根据二进制来分析.2的幂如2,4,8,等有一个特点: 二进制数首位为1,其他位为0,如2为10,4为 ...
 
随机推荐
- SecurCRT 远程linux 输入中文及 oracle 查询出文号问题
			
一. 首先确认你的linux是否设置了支持中文 cat /etc/sysconfig/i18n 其中: LANG 变量是 language 的简称, 这个变量时决定系统的默认语言, 即系统菜单, 程序 ...
 - java 读取world的图片 并把图片路径存入数据库
			
package World; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcep ...
 - [浪风分享] -- 系列百度云管家 V2.1.0 单文件版-分享必用的神器
			
废话不多说,神器送上 百度云管家 V2.1.0 更新时间:2013年05月22日更新内容:1.支持云端文件管理:删除.重命名.新建文件夹.移动2.支持缩略图浏览模式3.支持三种文件排序方式:按修改时间 ...
 - 嵌入式开发之davinci--- 8148/8168/8127 中swms、Mosaic’s、display 显示pal 模式
			
(1) (2) (3) (4) -------------------------author:pkf ------------------------------time:2-3 --------- ...
 - db2 blob  EMPTY_BLOB()
			
--针对BLOG类型的列,写入一个0长度的字符串
 - poj 2594(可相交的最小路径覆盖)
			
题目链接:http://poj.org/problem?id=2594 思路:本来求最小路径覆盖是不能相交的,那么对于那些本来就可达的点怎么处理,我们可以求一次传递闭包,相当于是加边,这样我们就可以来 ...
 - js 触摸事件 touch
			
//ban 为某div let startX = 0; ban.addEventListener("touchstart",function(){ //获取初始点击位置 start ...
 - Quartz.NET 作业调度
			
Quartz 简介: Quartz.NET是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等. Quartz.NET允许开发人员根据时间间隔(或天)来调 ...
 - IO-Polling的代码分析
			
在前一篇文章<IO-Polling实现分析与性能评測>中提到了IO-Polling与中断的原理差别,并通过两种模式下NVMe SSD的性能測试对两者进行了对照. 这篇文章将深入到IO-Po ...
 - Java的版本历史与特性
			
一个比较流行的问题是,“Java下一个版本会有什么特性呢?” .这是否是个好问题却有待商榷.在下面的内容里,我总结了至今为止的Java主要发行版中各自引入的新特性,这样做的目的是为了突出各个新特性是在 ...