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 ...
随机推荐
- Python: 去掉字符串中的非数字(或非字母)字符
>>> crazystring = ‘dade142.;!0142f[.,]ad’ 只保留数字>>> filter(str.isdigit, crazystring ...
- java基础讲解09-----接口,继承,多态
还有什么包装类,数字类,这些简单的我就不想过去介绍,前面也大概的介绍了下,继承,多态 1.类的继承 继承的思想:基于某个父类的扩展,制定一个新的子类.子类可以继承父类原有的属性,方法,也可以重写父类的 ...
- Asp.Net MVC之防止用户注入脚本参数
假设有一个Controller,代码如下: public string Browse(string genre) { string message = "Store.Browse, Genr ...
- unity3d常用控件
直接上代码,就能看懂了. private string txt1; private string pwd1; private int tool1; private bool isMuted; priv ...
- 数据库表syscolumns 各个字段含义 select * from syscolumns where name='textA'
每个数据库创建后都会有一些系统表用来存储该数据库的一些基本信息 每个表和视图中的每列在表中占一行,存储过程中的每个参数在表中也占一行.该表位于每个数据库中. 列名 数据类型 描述 name sysna ...
- MySQL主从(MySQL proxy Lua读写分离设置,一主多从同步配置,分库分表方案)
Mysql Proxy Lua读写分离设置 一.读写分离说明 读写分离(Read/Write Splitting),基本的原理是让主数据库处理事务性增.改.删操作(INSERT.UPDATE.DELE ...
- CentOS安装自动补全安装包
CentOS7标准版有这个功能,但是CentOS6却没有,其实很简单: 1.安装bash-completion yum install bash-completion 2.保存一下最新的缓存 yum ...
- Java接口的异常设计
一.问题的提出 疑惑1:在设计接口的时,对于接口方法何时需要声明抛出受检异常或者说所有的接口方法最后都声明抛出受检异常? 以下是代码片段: public interface xx{ public ...
- qt中执行 sql文件的方法
由于qt中没有原生的执行sql文件的方法.因此我们需要根据sql文件中的流的特点,将其分解成一个个语句单独执行. 1.首先通过Qfile读取sql文件 2.将sql文件中的内容通过“:”进行拆解 3. ...
- 浅谈P2P终结者原理及其突破
P2P终结者按正常来说是个很好的网管软件,但是好多人却拿它来,恶意的限制他人的流量,使他人不能正常上网,下面我们就他的功能以及原理还有突破方法做个详细的介绍! 我们先来看看来自在网上PSP的资料:P2 ...