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

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.

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

Leetcode: Text Justification. java

时间: 2024-08-28 20:34:24

Leetcode: Text Justification. java的相关文章

LeetCode: Text Justification [068]

[题目] 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. P

leetcode 68 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 ex

LeetCode: Text Justification 解题报告

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 i

[leetcode]Text Justification @ Python

原题地址:https://oj.leetcode.com/problems/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 approac

[LeetCode] Text Justification words显示的排序控制

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 ex

Text Justification leetcode 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. Pa

Java for LeetCode 068 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 ex

[LeetCode][JavaScript]Text Justification

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 i

LeetCode --- 68. Text Justification

题目链接: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