[LeetCode]68. Text Justification 深入浅出讲解和代码示例

来源:互联网 发布:在国外如何使用淘宝 编辑:程序博客网 时间:2024/06/05 03:54

1、汇总概要

本题涵盖了归一化的解题思路。


2、题目

DescriptionHintsSubmissionsSolutions

Discuss   Editorial Solution Pick One

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.

https://leetcode.com/problems/text-justification/#/description


3、审题

给一个字符串数组,将其归一化输出:每一行满足特定长度,字符显示两边对齐。


4、解题思路

1. 找到归为同一行的切分点。

    当前单词加入本行的条件,当前行总长度不超标,且再加入下一个后超标。

2. 同一行的单词确定后,需考虑每个单词间加入的空格数,以实现两边对齐的效果。

    设本行单词数=n,每行长度限制L,空格加入的位置有n-1个

     1) 当本行单词总长度+(n-1)个空格 == L时,不添加额外空格;

     2) 当本行单词总长度+(n-1)个空格 < L时,计算差值dif/(n-1):

对结果取整,即为n-1个空格处平均额外添加的空格个数;

对结果取余(设为nPre),即为n-1个空格位置的前nPre个,需再额外添加一个空格;(当可以整除时,余数为0)


举例说明,设改行有3个(n=3)单词,单词长度分别是2、2、3即:'xx xx xxx' (此时本行单词基本总长度=9)

空格位置=n-1=2,即为实现两边对齐,有2个位置可以放置空格。

dif=L-9=16-9=7,则这7个空格需填到2个位置处:

对dif/(n-1)=7/2 取整,结果为3,即两个位置各+3个空格;

对dif/(n-1)=7/2取余,结果=1,即两个位置中前1个需多加1个空格。

总结下来:空格位置1,额外添加4个空格,即共5个空格(之前有一个基本的分割空格);空格位置2,额外添加3个空格,即共4个空格:

结果='xx     xx    xxx'    


5、代码示例 - Java

public class TextJustification {public String getSpace(int len){String space = "";for(int i=0;i<len;i++){space = space + " ";}return space;}public ArrayList fullJustification(String [] textStr, int L){ArrayList <String>res = new ArrayList<String>();int lenStr = textStr.length;int lensum = 0;int lensumNext = 0;int start = 0;String line="";int spaceNum = 1;for(int i=0;i<lenStr;i++){if(i>0 && lensum>0){ //here 1 is the space,which to split wordslensum = lensum + 1;}int lencur = textStr[i].length();lensum = lensum + lencur;if(i<lenStr-1){lensumNext = lensum + textStr[i+1].length()+1;}else{lensumNext = lensum;}if (lensumNext > L && i<lenStr-1 || lensumNext<L && i==lenStr-1 || lensum==L){//get the boundaryif(i<lenStr-1){spaceNum = i - start;}else{spaceNum = 1;}int spaceLen;if(spaceNum!=0){spaceLen = (L-lensum)/spaceNum;}else{spaceLen = 0;} int preSpace = L-lensum - spaceNum*spaceLen; //the num whose spacelen need + 1String spaceOri = getSpace(spaceLen);String spaceUse;for(int j=start;j<=i;j++){if(j<start+preSpace && i<lenStr-1){spaceUse = spaceOri+"  ";}else if(j == i && i<lenStr-1){spaceUse = "";}else if(j==lenStr-1){spaceUse = spaceOri;}else{spaceUse = spaceOri+" ";}line = line+textStr[j]+spaceUse;}res.add(line);start = i+1; //update start indexline = ""; //clear linelensum = 0;}}return res;}public static void main(String[] args){String[] textStr = {"This", "is", "an", "example", "of", "text","a","justification."};for(int i=0;i<textStr.length;i++){System.out.println(textStr[i]);}TextJustification tj = new TextJustification();int L = 16;ArrayList res = new ArrayList();res = tj.fullJustification(textStr,L);for(int i=0;i<res.size();i++){System.out.println(res.get(i));}}}


---------------------------------------------------------------------------------------------------

本文链接:http://blog.csdn.net/karen0310/article/details/xx

请尊重作者的劳动成果,转载请注明出处!

---------------------------------------------------------------------------------------------------