forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAC_simulation_n.java
63 lines (56 loc) · 1.8 KB
/
AC_simulation_n.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_simulation_n.java
* Create Date: 2015-03-04 14:55:03
* Descripton:
*/
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Solution {
private String getOneLine(String[] words, int start, int end, int rest, int L) {
StringBuilder line = new StringBuilder(words[start]);
rest += (end - start);
int even_space = rest + 1, num_of_more = 0;
if (start != end) {
even_space = rest / (end - start);
num_of_more = rest - even_space * (end - start);
}
for (int i = start + 1; i <= end; ++i) {
for (int j = 0; j < even_space; ++j) {
line.append(' ');
}
if (i - start <= num_of_more)
line.append(' ');
line.append(words[i]);
}
while (line.length() < L) {
line.append(' ');
}
return line.toString();
}
public List<String> fullJustify(String[] words, int L) {
List<String> ret = new ArrayList<String>();
int sz = words.length;
int start_pos = 0;
int cur_len = 0;
for (int i = 0; i < sz; ++i) {
if (cur_len + words[i].length() > L) {
ret.add(getOneLine(words, start_pos, i - 1, L - cur_len + 1, L));
start_pos = i;
cur_len = 0;
}
cur_len += words[i].length();
cur_len += 1;
}
ret.add(getOneLine(words, start_pos, sz - 1, 0, L));
return ret;
}
// debug
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
Solution s = new Solution();
int[] input = {1, 2, 3, 1};
System.out.println("no case");
}
}