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. Android权限说明 system权限 root权限

    原文链接:http://blog.csdn.net/rockwupj/article/details/8618655 Android权限说明 Android系统是运行在Linux内核上的,Androi ...

  2. java基础讲解12-----Swing

    package com.swing; import java.awt.*; import javax.swing.*; public class Swing01  extends JFrame{ /* ...

  3. 物联网通信协议——比较-MQTT、 DDS、 AMQP、XMPP、 JMS、 REST、 CoAP

    物联网通信协议——比较-MQTT. DDS. AMQP.XMPP. JMS. REST. CoAP   AMQP & MQTT & DDS (https://www.youtube.c ...

  4. 转 OAuth 2.0授权协议详解

    http://www.jb51.net/article/54948.htm 作者:阮一峰 字体:[增加 减小] 类型:转载 时间:2014-09-10我要评论 这篇文章主要介绍了OAuth 2.0授权 ...

  5. (3)Smali系列学习之Smali语法详解

    数据类型 Dalvik字节码只有两种格式:基本类型和引用类型.对象和数组属于引用类型 语法 含义 V void,只用于返回值类型 Z boolean B byte S short C char I i ...

  6. WebApi接口安全认证——HTTP之摘要认证

    摘要访问认证是一种协议规定的Web服务器用来同网页浏览器进行认证信息协商的方法.它在密码发出前,先对其应用哈希函数,这相对于HTTP基本认证发送明文而言,更安全.从技术上讲,摘要认证是使用随机数来阻止 ...

  7. Monotone Chain Convex Hull(单调链凸包)

    Monotone Chain Convex Hull(单调链凸包)算法伪代码: //输入:一个在平面上的点集P //点集 P 按 先x后y 的递增排序 //m 表示共a[i=0...m]个点,ans为 ...

  8. cocoa pods 升级遇到的问题

    1. cocoa pods 升级遇到的问题 运行 sudo gem update --system 报错 Updating rubygems-update ERROR: While executing ...

  9. IPC之信号量

    无名信号量 POSIX标准提出了有名信号量和无名信号量来同步进程和线程,而linux(2.6以前)只实现了无名信号量. sem_overview中有详细介绍:man 7 sem_overview. S ...

  10. bcrypt install `node-pre-gyp install --fallback-to-build`

    npm安装parse-server的过程中遇到了2次错误 尝试1 ganiks@ganiks-ubuntu-trusty-64:~$ sudo npm i -g parse-server npm WA ...