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的更多相关文章

  1. [LeetCode] Text Justification 文本左右对齐

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

  2. [leetcode]Text Justification @ Python

    原题地址:https://oj.leetcode.com/problems/text-justification/ 题意: Given an array of words and a length L ...

  3. LeetCode:Text Justification

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

  4. LeetCode: Text Justification 解题报告

    Text Justification Given an array of words and a length L, format the text such that each line has e ...

  5. [Leetcode] text justification 文本对齐

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

  6. LeetCode OJ-- Text Justification

    https://oj.leetcode.com/problems/text-justification/ 细节题 class Solution { public: vector<string&g ...

  7. [LeetCode] Text Justification words显示的排序控制

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

  8. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  9. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

  10. leetcode@ [68] Text Justification (String Manipulation)

    https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...

随机推荐

  1. JBoss jar包冲突及jar加载顺序

    http://blog.163.com/javaee_chen/blog/static/17919507720116149511489/将一个完整的.war包部署到Jboss容器中,启动后报如下错误: ...

  2. T-sql for xml path使用

    用法: FOR XML PATH 方法是用于将查询结果集以XML形式展示 sql: p.ContactTypeID,p.ModifiedDate,p.Name from [Person].[Conta ...

  3. 电子商务(电销)平台中商品模块(Product)数据库设计明细(转载)

    电子商务(电销)平台中商品模块(Product)数据库设计明细 以下是自己在电子商务系统设计中的数据库设计经验总结,而今发表出来一起分享,如有不当,欢迎跟帖讨论~ 商品表 (product)|-- 自 ...

  4. 全面进攻python之前回顾下自己近三个月的自学之路

    人生是在一直试错的过程中成长起来的.这句话貌似很有道理,但回顾了下自己近三个月python自学学习之路,又觉得自己对这句话又有了新的看法------行动之前必须要有正确的选择,这样做错了才能成长. 2 ...

  5. GNU--gprof使用总结

    Added macros ACE_USES_GPROF which enables users to use gprof in a multithreaded environment with ACE ...

  6. Oracle 性能调优案例(代码级别)

    业务案例一: 业务:千万记录表中查询出50条符合条件的记录. 现象:oracle部署时跨机器,业务取得数据耗时10ms.造成业务性能不达标. 为了突出主题,对于异常分支,均已省略. 对于通常写法, o ...

  7. epoll反应堆模型

    ================================ 下面代码实现的思想:epoll反应堆模型:( libevent 网络编程开源库 核心思想) 1. 普通多路IO转接服务器: 红黑树 ― ...

  8. App登录注册功能,怎样做到用户体验最佳?

    用户登录系统,可以细分为三项功能模块,分别是:登录.注册和密码找回.本文作者将结合自身经历,谈谈他在做这块的时候一些想法,主要是涉及业务流程. 登录和注册功能,不论是PC端还是移动端,大多数产品都会涉 ...

  9. Atitit.java的浏览器插件技术 Applet japplet attilax总结

    Atitit.java的浏览器插件技术  Applet  japplet attilax总结 1. Applet类及各个方法说明 1 2. JApplet类示例 2 3. / 用main方法运行JAp ...

  10. atitit.浏览器web gui操作类库 和 操作chrome浏览器的类库使用总结

    atitit.浏览器web gui操作类库 和 操作chrome浏览器的类库使用总结 1. 常见标准 1 1.1. 录制重放 1 1.2. 一个窗体一个proxy cookie 1 1.3. exec ...