频道栏目
首页 > 资讯 > C++ > 正文

LeetCode | Text Justification

14-03-14        来源:[db:作者]  
收藏   我要投稿

题目

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."]
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.

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. 分析

    按照题目要求完成即可,注意corner cases

    代码

    import java.util.ArrayList;
    
    public class TextJustification {
    	public ArrayList fullJustify(String[] words, int L) {
    		ArrayList results = new ArrayList();
    
    		int wordsLen = 0;
    		int head = 0;
    		for (int i = 0; i < words.length; ++i) {
    			if (head == i) {
    				if (i == words.length - 1) {
    					StringBuilder lineBuilder = new StringBuilder();
    					lineBuilder.append(words[i]);
    					int remain = L - lineBuilder.length();
    					for (int k = 0; k < remain; ++k) {
    						lineBuilder.append(" ");
    					}
    					results.add(lineBuilder.toString());
    				} else {
    					wordsLen += words[i].length();
    				}
    			} else {
    				if (wordsLen + words[i].length() + 1 > L) {
    					StringBuilder lineBuilder = new StringBuilder();
    					int intervalNum = i - head - 1;
    					if (intervalNum == 0) {
    						// corner case
    						lineBuilder.append(words[i - 1]);
    						int remain = L - lineBuilder.length();
    						for (int k = 0; k < remain; ++k) {
    							lineBuilder.append(" ");
    						}
    						results.add(lineBuilder.toString());
    					} else {
    						int totalSpaceNum = L - (wordsLen - intervalNum);
    						int redundantSpaceNum = totalSpaceNum % intervalNum;
    						int spaceNumPerInterval = totalSpaceNum / intervalNum;
    						for (int j = head; j < i - 1; ++j) {
    							lineBuilder.append(words[j]);
    							for (int k = 0; k < spaceNumPerInterval; ++k) {
    								lineBuilder.append(" ");
    							}
    							if (j - head < redundantSpaceNum) {
    								lineBuilder.append(" ");
    							}
    						}
    						lineBuilder.append(words[i - 1]);
    						results.add(lineBuilder.toString());
    					}
    					// new line
    					head = i;
    					wordsLen = 0;
    					--i;
    				} else if (wordsLen + words[i].length() + 1 == L) {
    					StringBuilder lineBuilder = new StringBuilder();
    					for (int j = head; j <= i; ++j) {
    						lineBuilder.append(words[j]).append(" ");
    					}
    					results.add(lineBuilder.substring(0,
    							lineBuilder.length() - 1));
    
    					// new line
    					head = i + 1;
    					wordsLen = 0;
    				} else {
    					if (i == words.length - 1) {
    						StringBuilder lineBuilder = new StringBuilder();
    						for (int j = head; j <= i; ++j) {
    							lineBuilder.append(words[j]).append(" ");
    						}
    						int remain = L - lineBuilder.length();
    						for (int k = 0; k < remain; ++k) {
    							lineBuilder.append(" ");
    						}
    						results.add(lineBuilder.toString());
    					} else {
    						wordsLen += words[i].length() + 1;
    					}
    				}
    			}
    		}
    		return results;
    	}
    }
相关TAG标签
上一篇:复杂链表的复制
下一篇:HDU1698(线段树区间更新求和)
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站