Monthly Archives: August 2014

Remove Duplicates from Sorted Array


Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2]. read more

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 exactly L 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 Lin length.

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.

My Solution

public List<String> fullJustify(String[] words, int L) { if (words == null || words.length == 0) { return Collections.emptyList(); } List<Line> lines = parseToLines(words, L); return fillLines(lines, L); } private List<String> fillLines(List<Line> lines, int L) { List<String> output = new ArrayList<String>(); for (int i = 0; i < lines.size(); i++) { Line line = lines.get(i); if (line.wordCount() == 1) { StringBuilder sb = new StringBuilder(); sb.append(line.words.get(0)); sb.append(getSlot(L - line.totalNonSpaceLetterLength())); output.add(sb.toString()); } else if (i == lines.size() - 1) { StringBuilder sb = new StringBuilder(); sb.append(line.words.get(0)); for (int j = 1; j < line.wordCount(); j++) { sb.append(' '); sb.append(line.words.get(j)); } sb.append(getSlot(L - line.minimumLength())); output.add(sb.toString()); } else { int space = (L - line.totalNonSpaceLetterLength()) / (line.wordCount() - 1); int remain = (L - line.totalNonSpaceLetterLength()) % (line.wordCount() - 1); StringBuilder sb = new StringBuilder(); sb.append(line.words.get(0)); for (int j = 1; j <= remain; j++) { sb.append(getSlot(space+1)); sb.append(line.words.get(j)); } for (int j = remain+1; j < line.wordCount(); j++) { sb.append(getSlot(space)); sb.append(line.words.get(j)); } output.add(sb.toString()); } } return output; } private String getSlot(int i) { char[] cs = new char[i]; Arrays.fill(cs, ' '); return new String(cs); } private List<Line> parseToLines(String[] words, int L) { List<Line> lines = new ArrayList<Line>(); Line currentLine = new Line(); lines.add(currentLine); for (String word : words) { if (currentLine.minimumLength() + word.length() < L) { currentLine.addWord(word); } else { currentLine = new Line(); lines.add(currentLine); currentLine.addWord(word); } } return lines; } class Line { private List<String> words; private int totalLength; Line() { words = new ArrayList<String>(); totalLength = 0; } void addWord(String word) { words.add(word); totalLength += word.length(); } int wordCount() { return words.size(); } int totalNonSpaceLetterLength() { return totalLength; } int minimumLength() { return totalLength + words.size() - 1; } } read more

[转] 胡适在北京大学毕业典礼上的讲话(1932年6月27日)