Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc013ad3

Browse files
author
applewjg
committed
Text Justification
Change-Id: I258afaa8e7106642845c136a0d32d277282beca0
1 parent40a5e0f commitc013ad3

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

‎TextJustification.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Author: Andy, nkuwjg@gmail.com
3+
Date: Jan 29, 2015
4+
Problem: Text Justification
5+
Difficulty: Hard
6+
Source: https://oj.leetcode.com/problems/text-justification/
7+
Notes:
8+
Given an array of words and a length L, format the text such that each line has exactly L
9+
characters and is fully (left and right) justified.
10+
You should pack your words in a greedy approach; that is, pack as many words as you can in each line.
11+
Pad extra spaces ' ' when necessary so that each line has exactly L characters.
12+
Extra spaces between words should be distributed as evenly as possible. If the number of spaces
13+
on a line do not divide evenly between words, the empty slots on the left will be assigned more
14+
spaces than the slots on the right.
15+
For the last line of text, it should be left justified and no extra space is inserted between words.
16+
17+
For example,
18+
words: ["This", "is", "an", "example", "of", "text", "justification."]
19+
L: 16.
20+
Return the formatted lines as:
21+
[
22+
"This is an",
23+
"example of text",
24+
"justification. "
25+
]
26+
Note: Each word is guaranteed not to exceed L in length.
27+
Corner Cases:
28+
A line other than the last line might contain only one word. What should you do in this case?
29+
In this case, that line should be left-justified.
30+
31+
Solution: ...
32+
*/
33+
34+
publicclassSolution {
35+
publicList<String>fullJustify(String[]words,intL) {
36+
List<String>res =newArrayList<String>();
37+
inti =0,N =words.length;
38+
while (i <N) {
39+
intlength =words[i].length();
40+
intj =i +1;
41+
while (j <N &&length +words[j].length() + (j-i) <=L) {
42+
length +=words[j++].length();
43+
}
44+
StringBuilders =newStringBuilder(words[i]);
45+
booleanisLastLine = (j==N);
46+
booleanoneWord = (j ==i +1);
47+
intaverage = (isLastLine ||oneWord) ?1 : (L-length)/(j-i-1);
48+
intextra = (isLastLine ||oneWord) ?0 : (L-length)%(j-i-1);
49+
for (intk =i +1;k <j; ++k) {
50+
char[]tmp =newchar[extra>0?average+1:average];
51+
Arrays.fill(tmp,' ');
52+
s.append(tmp);
53+
s.append(words[k]);
54+
extra--;
55+
}
56+
char[]tmp =newchar[L -s.length()];
57+
Arrays.fill(tmp,' ');
58+
s.append(tmp);
59+
res.add(s.toString());
60+
i =j;
61+
}
62+
returnres;
63+
}
64+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp