LeetCode OJ算法题(六十八):Text 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 exactlyL 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."]
L16.

Return the formatted lines as:

[
   "This    is    an",
   "example  of text",
   "justification.  "
]

Note: Each word is guaranteed not to exceed L in length.

click to show corner cases.

Corner Cases:

  • A line other than the last line might contain only one word. What should you do in this case?
    In this case, that line should be left-justified.
解法:

本题要求对字符串进行排版,第一反应就是根据L计算出每一行能够包含的字符串个数,注意单词间至少会有一个长度为1的空格符。假设字符串个数为从i到j-1,那么计算剩下的空格应该有多长,用(L-sum(i,j-1))/ (i-j+1)可以求出每个间隔内应该包含多少个空格符。如果不能整除,那个第一个间隔的长度就在前式的基础上加1,然后更新剩余的总共空格数以及间隔数,然后计算第二个间隔的长度,以此类推,知道计算出所有间隔长度,这个时候就可以得到一行的结果了。

注意如果一行只有一个单词,那么采用左对齐策略,其后补上空格直至长度为L,最后一行也一样。

import java.util.ArrayList;
import java.util.List;


public class No68_TextJustification {
	public static void main(String[] args){
//		System.out.println(fullJustify(new String[]{"This", "is", "an", "example", "of", "text", "justification."}, 16));
//		System.out.println(fullJustify(new String[]{"a","b","c","d","e"}, 3));
		System.out.println(fullJustify(new String[]{"My","momma","always","said,","\"Life","was","like","a","box","of","chocolates.","You","never","know","what","you're","gonna","get."}, 20));
	}
	public static List<String> fullJustify(String[] words, int L) {
        List<String> ret = new ArrayList<String>();
        int i = 0;
        while(i < words.length){
        	StringBuffer element = new StringBuffer();
        	int len = words[i].length();
        	int j = i+1;
        	while(j<words.length && len+1+words[j].length() <= L){
        		len += words[j].length()+1;
        		j++;
        	}
        	if(j == words.length){
        		for(int p=i;p<j-1;p++){
        			element.append(words[p]);
        			element.append(" ");
        		}
        		element.append(words[j-1]);
        		int m = element.length();
        		for(int p=0;p<L-m;p++){
         			element.append(" ");
        		}
        		ret.add(element.toString());
        		break;
        	}
        	int totalblank = L-len+j-i-1;
        	if(i+1==j){
//        		只有一个单词时居中,但题目要求左对齐,擦!
//        		for(int p=0;p<(totalblank+1)/2;p++) element.append(" ");
//    			element.append(words[i]);
//    			for(int p=0;p<totalblank/2;p++) element.append(" ");
        		element.append(words[i]);
        		int m = element.length();
        		for(int p=0;p<L-m;p++){
         			element.append(" ");
        		}
        	}
        	else{
        		element.append(words[i]);
        		int blank = totalblank%(j-1-i)==0?totalblank/(j-1-i):totalblank/(j-1-i)+1;
        		for(int p=i+1;p<j;p++){
        			for(int q=0;q<blank;q++) element.append(" ");
        			element.append(words[p]);
        			if(p != j-1){
        				totalblank = totalblank - blank;
            			blank = totalblank%(j-1-p)==0?totalblank/(j-1-p):totalblank/(j-1-p)+1;
        			}
        		}
        	}
        	ret.add(element.toString());
        	i = j;
        }
        return ret;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值