-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSolution.java
107 lines (101 loc) · 3.79 KB
/
Solution.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package _044;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2017/10/16
* desc :
* </pre>
*/
public class Solution {
// public boolean isMatch(String s, String p) {
// if (p.length() == 0) return s.length() == 0;
// int si = 0, pi = 0, match = 0, star = -1;
// int sl = s.length(), pl = p.length();
// char[] sc = s.toCharArray(), pc = p.toCharArray();
// while (si < sl) {
// if (pi < pl && (pc[pi] == sc[si] || pc[pi] == '?')) {
// si++;
// pi++;
// } else if (pi < pl && pc[pi] == '*') {
// star = pi++;
// match = si;
// } else if (star != -1) {
// si = ++match;
// pi = star + 1;
// } else return false;
// }
// while (pi < pl && pc[pi] == '*') pi++;
// return pi == pl;
// }
// public boolean isMatch(String s, String p) {
// if (p.length() == 0) return s.length() == 0;
// int sl = s.length(), pl = p.length();
// boolean[][] dp = new boolean[sl + 1][pl + 1];
// char[] sc = s.toCharArray(), pc = p.toCharArray();
// dp[0][0] = true;
// for (int i = 1; i <= pl; ++i) {
// if (pc[i - 1] == '*') dp[0][i] = dp[0][i - 1];
// }
// for (int i = 1; i <= sl; ++i) {
// for (int j = 1; j <= pl; ++j) {
// if (pc[j - 1] != '*') {
// dp[i][j] = dp[i - 1][j - 1] && (sc[i - 1] == pc[j - 1] || pc[j - 1] == '?');
// } else {
// dp[i][j] = dp[i][j - 1] || dp[i - 1][j];
// }
// }
// }
// return dp[sl][pl];
// }
public List<String> fullJustify(String[] words, int maxWidth) {
int len = words.length;
if (len == 0) return Collections.emptyList();
List<String> ans = new ArrayList<>();
StringBuilder spaces = new StringBuilder();
for (int i = 0; i < maxWidth; ++i) {
spaces.append(" ");
}
int sLen = -1, left = 0;
for (int i = 0; i < len; ++i) {
if (sLen + words[i].length() + 1 <= maxWidth) {
sLen += words[i].length() + 1;
} else {
StringBuilder sub = new StringBuilder(words[left]);
int rest = maxWidth - sLen;
int seg = i - left;
if (seg == 0) {
sub.append(spaces.substring(0, rest));
} else {
int leastSpace = rest / seg + 1;
int restSpace = rest % seg;
for (int j = left + 1; j < i; ++j) {
if (restSpace-- > 0) {
sub.append(spaces.substring(0, leastSpace + 1)).append(words[j]);
} else {
sub.append(spaces.substring(0, leastSpace)).append(words[j]);
}
}
}
ans.add(sub.toString());
left = i;
sLen = words[i].length();
}
}
StringBuilder sub = new StringBuilder(words[left]);
for (int i = left + 1; i < len; ++i) {
sub.append(" ").append(words[i]);
}
ans.add(sub + spaces.substring(0, maxWidth - sub.length()));
return ans;
}
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.fullJustify(new String[]{"", ""}, 0));
System.out.println(solution.fullJustify(new String[]{"a"}, 1));
System.out.println(solution.fullJustify(new String[]{"This", "is", "an", "example", "of", "text", "justification."}, 16));
}
}