程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> LeetCode | Text Justification

LeetCode | Text Justification

編輯:C++入門知識

題目

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;
    	}
    }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved