Leetcode-Test 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.
Analysis:
For each line, the first word does not need space, for each of the following words, we need count one space. In this way, we find out the words fit into the current line, then construct the string of the current line.
After finding out all words in the current line, we calculate the number of space between each words. Suppose the total number of space is totalSpace, then we should add (totalSpace/(wordNum-1)) between words. In addition, we have (totalSpace%(wordNum-1)) number of space left, we add them from left to right into each gap. NOTE: a special case is that there is only one word in the current line, we then add all spaces after the word.
For the last line, we just add one space in each gap and then add the left spaces on the end of the line.
Solution:
public class Solution {
public List<String> fullJustify(String[] words, int L) {
List<String> res = new ArrayList<String>();
int index = 0;
int totalSpace=-1, aveSpace=-1, leftSpace=-1;
while (index<words.length){
int curLen = 0;
List<String> lineWord = new ArrayList<String>();
lineWord.add(words[index]);
curLen = words[index].length();
index++;
while (index<words.length && curLen+words[index].length()+1<=L){
lineWord.add(words[index]);
curLen += words[index].length()+1;
index++;
}
int wordNum = lineWord.size();
//if it is not the last line.
if (index<words.length){
if (wordNum>1){
totalSpace = L-curLen+(wordNum-1);
aveSpace = totalSpace/(wordNum-1);
leftSpace = totalSpace%(wordNum-1);
String space = "";
for (int i=0;i<aveSpace;i++) space = space+" ";
String lineStr = lineWord.get(0);
for (int i=1;i<lineWord.size();i++){
lineStr += space;
if (leftSpace>0){
lineStr += " ";
leftSpace--;
}
lineStr += lineWord.get(i);
}
res.add(lineStr);
} else {
totalSpace = L-lineWord.get(0).length();
String lineStr = lineWord.get(0);
for (int i=0;i<totalSpace;i++) lineStr += " ";
res.add(lineStr);
}
} else {
leftSpace = L-curLen;
String lineStr = lineWord.get(0);
for (int i=1;i<wordNum;i++) lineStr += " "+lineWord.get(i);
for (int i=0;i<leftSpace;i++) lineStr += " ";
res.add(lineStr);
}
}
return res;
}
}
Leetcode-Test Justification的更多相关文章
- [LeetCode] Text Justification 文本左右对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- [leetcode]Text Justification @ Python
原题地址:https://oj.leetcode.com/problems/text-justification/ 题意: Given an array of words and a length L ...
- LeetCode:Text Justification
题目链接 Given an array of words and a length L, format the text such that each line has exactly L chara ...
- LeetCode: Text Justification 解题报告
Text Justification Given an array of words and a length L, format the text such that each line has e ...
- [Leetcode] text justification 文本对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- LeetCode OJ-- Text Justification
https://oj.leetcode.com/problems/text-justification/ 细节题 class Solution { public: vector<string&g ...
- [LeetCode] Text Justification words显示的排序控制
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
- leetcode@ [68] Text Justification (String Manipulation)
https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...
随机推荐
- struts过滤器的不同2.16以后应该是: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 2.12以前应该是org.apache.struts2.dispatcher.Filterdispatcher
版本不同过滤器不同.2.16以后应该是:org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter2.12以前应该是or ...
- 【转】火星坐标、百度坐标、WGS-84坐标相互转换及墨卡托投影坐标转经纬度JavaScript版
原文地址:https://www.cnblogs.com/fwc1994/p/5884115.html 火星坐标 火星坐标是国家测绘局为了国家安全在原始坐标的基础上进行偏移得到的坐标,基本国内的电子地 ...
- 扩展方法 DataTable的ToList<T>
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.T ...
- 悟道—位IT高管20年的职场心经(读书笔记一)
悟道--一位IT高管20年的职场心经 第一章 修炼! 修炼! 别跟我谈事业,先把工作做好. 别跟我说理想,先把饭碗端好: 志不可天天立.道必须日日修.没有实力,一切皆为妄谈. 修炼是硬道理. 1.1 ...
- Win7下更改iTunes备份路径最便捷的方法
① 先删除C:\Users\你的用户名\AppData\Roaming\Apple Computer里的 MobileSync文件夹(首次安装iTunes要先运行一次itunes,才有这个文件夹,如果 ...
- ClientViaBehavior行为
ClientViaBehavior行为: 紧接红框:方式,也支持配置的应用方式
- 消息成功失败回调demo
) )); try {// ListenableFuture<ResponseEntity<String>> future = restTemplate.postForEnti ...
- scut协议配置工具初始化的一些问题
1.如果点击协议配置工具左上角的项目按钮没反应,说明数据库没有正确配置. 2.数据库创建脚本运行如果乱码,把utf-8改成ansi 3.如果ContractDB不存在就自行创建数据库. 4.使用sql ...
- excel增加上一列的数值(日期)
=TEXT(D2-1,"m月d日") 有年的话就是 =TEXT(D2-1,"yyyy年m月d日") D2就是参照日期
- 浅谈HTTPS协议和SSL、TLS之间的区别与关系
HTTP可能是我们见到过最多的一个字符串了,应该没有之一,而对于HTTPS到来和趋势,我们又开始看到SSL/TLS,所以对于一般不只做技术的人来说这或许还是一个疑问,那么子凡就趁最近在折腾这方面来给大 ...