-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leetcode068.java
85 lines (75 loc) · 2.81 KB
/
Leetcode068.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package test;
//文字对齐模式,难点在思考清楚所有情况,方法为greedy,遍历所有,当超出范围则加入List并重新开始
public class Leetcode068 {
//存储答案
List<String> ans = new ArrayList<String>();
//主方法,讨论base case然后调用greedy方法
public List<String> fullJustify(String[] words, int maxWidth) {
if (maxWidth == 0) {
ans.add("");
return ans;
}
if (words[0].length() == 0) {
ans.add(addSpace(maxWidth));
return ans;
}
fullJstifyGreedy(words, maxWidth, 0);
return ans;
}
//greedy方法
public void fullJstifyGreedy(String[] words, int maxWidth, int start) {
//length存储当前长度,spacelength存储空格长度,newstart进入递归,count存储需要几个长空格,temp存储该次递归需要加入的字符串
int length = words[start].length();
int spacelength = 0;
int newstart = 0;
int count = 0;
String temp = words[start];
//从第二位开始循环
for (int i=start+1; i<words.length; i++) {
//当小于时更新length,当是最后一个时候加入temp,当大于时加入新String
if (length + 1 + words[i].length() <= maxWidth) {
length = length + 1 + words[i].length();
if (i == words.length-1) {
for (int j=start+1; j<=i; j++)
temp += ' ' + words[j];
}
} else {
if (i-start > 1) {
spacelength = (maxWidth - length + i - 1 - start) / (i - start - 1);
count = (maxWidth - length + i - 1 - start) % (i - start - 1);
if (count > 0)
temp = temp + addSpace(1+spacelength);
else
temp = temp + addSpace(spacelength);
} else
temp = temp + addSpace(maxWidth - length);
for (int j=start+1; j<i; j++) {
temp = temp + words[j];
if (j != i-1) {
if (j-start < count)
temp = temp + addSpace(1 + spacelength);
else
temp = temp + addSpace(spacelength);
}
}
ans.add(temp);
newstart = i;
break;
}
}
//当是最后一个时补上空格并加入,否则递归下一轮
if (newstart == 0) {
temp = temp + addSpace(maxWidth-length);
ans.add(temp);
return;
} else
fullJstifyGreedy(words, maxWidth, newstart);
}
//增加特定数量的空格
public String addSpace(int a) {
String temp = "";
for (int i=0; i<a; i++)
temp += ' ';
return temp;
}
}