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 ...
随机推荐
- 【Shiro】Apache Shiro架构之自定义realm
[Shiro]Apache Shiro架构之身份认证(Authentication) [Shiro]Apache Shiro架构之权限认证(Authorization) [Shiro]Apache S ...
- 解决在开发环境中访问json配置文件时报HTTP 错误 404.3 - Not Found
问题描述: 在做demo时,使用json文件做配置文件,访问时,http状态提示:404.3错误,经验证json确认存在,路径也没有问题, 在浏览器地址栏里直接访问json时也提示同样的错误, 根据错 ...
- PayPal 对接
时间:2019-1-29 15:39:39 文章相应链接: 官方SDK:https://developer.paypal.com/docs/api/rest-sdks/ 官方API:https://d ...
- Xgboost 模型保存和载入()
https://blog.csdn.net/u012884015/article/details/78653178 xgb_model.get_booster().save_model('xgb.mo ...
- js对象成员的访问
var obj={0:1,1:'id'};//访问对象 obj['0'];//用[]来访问,不要用.的方式访问 -----对象与字符串的转换----- node.js JS对象和JSON字符串之间的转 ...
- MySQL之mysql客户端工作的批处理一些使用手法
通常我们会用mysql这个客户端程序来连接mysql库.这个通常是工作在交互式模式下的.如我们连接上mysql并执行如下操作: mysql -uroot -h127. -P3306 Welcome t ...
- unity, 什么时候用静态类,什么时候用单例
如果没有成员变量,或者成员变量都是常量,则用静态类. 如果有成员变量,则用单例.(以便让成员变量有初始化机会). //静态类 public class CmyFuncs{ public float m ...
- object-c输出对象
有时候在xcode里打断点很不准,看到对象总是nil,还是用打log比较靠谱: NSLog(@"obj info:%@",obj);
- Atitit.js模块化 atiImport 的新特性javascript import
Atitit.js模块化 atiImport 的新特性javascript import 1. 常见的js import规范amd ,cmd ,umd1 1.1. Require更多流行3 2. at ...
- JAVA设计模式之 命令模式【Command Pattern】
一.概述 命令模式能够将请求发送者和接收者全然解耦.发送者与接收者之间没有直接引用关系,发送请求的对象仅仅须要知道怎样发送请求,而不必知道怎样完毕请求.核心在于引入了命令类,通过命令类来减少发送者和接 ...