首页 > 代码库 > Leetcode: Text Justification. java

Leetcode: Text Justification. java

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.

public class Solution {
   public List<String> fullJustify(String[] words, int L) {
        List<String> list = new ArrayList<String> ();
        int size = words.length;
        int currentLength = 0;
        String tmp = "";
        
        //list = "This is an ", "example of text ", "justification. "
        for (int i = 0; i < size; i++) {
            currentLength += words[i].length() + 1;
            if (currentLength > L + 1) {
                list.add(tmp);
                tmp = "";
                currentLength = 0;
                --i;
            }
            else {
                tmp += words[i] + " ";
            }
        }
        //the last word. "justificaion. "
        if (!tmp.equals(""))
        	list.add(tmp);
        	
        	
        for (int i = 0; i < list.size() - 1; i++) {
            //i = 1, tmp = "example of text "
        	tmp = list.get(i);
        	String [] tmpStrArray = tmp.split(" ");
        	int totoalLength = 0;
        	for (int j = 0; j < tmpStrArray.length; ++j) {
        		totoalLength += tmpStrArray[j].length();
        	}
        	
        	//totoalLength = 13, L = 16, numOfString = 3, freeSpace = 16 - 13 = 3.
        	//spaceCount = {2, 1, 0}
        	int[] spaceCount = getSpaceCount(L - totoalLength, tmpStrArray.length);
        	tmp = "";
        	for (int j = 0; j < tmpStrArray.length; j++) {
        		tmp += tmpStrArray[j];
        		for (int k = 0; k < spaceCount[j]; ++k) {
        			tmp += " ";
        		}
        	}
        	
        	//tmp = "example  of text"
        	list.set(i, tmp);
        }
        
        //last word. no extra space between words.
        tmp = list.get(list.size() - 1);
        if (tmp.length() < L) {
        	while (tmp.length() < L) {
        		tmp += " ";
        	}
        }
        else if (tmp.length() > L) {
        	tmp = tmp.substring(0, L);
        }
        list.set(list.size() - 1, tmp);
        return list;
    }
    
    public int[] getSpaceCount(int freeSpace, int numOfString) {
    	int size = numOfString - 1;
    	int[] ret = new int[size + 1];
    	if (size == 0) {
    		ret [0] = freeSpace;
    	}
    	else {
    		for (int i = 0; i < ret.length - 1; i++) {
    			ret[i] = freeSpace % size == 0 ? freeSpace / size : freeSpace / size + 1;
    			freeSpace -= ret[i];
    			--size;
    		}
    	}
    	return ret;
    }
}