feat: 1.3.0

1.优化代码
This commit is contained in:
www.byteblogs.com 2023-05-21 22:03:05 +08:00
parent 5f18a0e0c9
commit 3b9f62a8e2

View File

@ -21,73 +21,6 @@ import java.util.List;
@Slf4j @Slf4j
public class EasyRetryEndListener implements ApplicationListener<ContextClosedEvent> { public class EasyRetryEndListener implements ApplicationListener<ContextClosedEvent> {
static class Solution {
public static void main(String[] args) {
System.out.println(fullJustify(new String[]{"This", "is", "an", "example", "of", "text", "justification."}, 16));
}
public static List<String> fullJustify(String[] words, int maxWidth) {
List<String> result = new ArrayList<>();
int n = words.length;
int start = 0; // 当前行的起始单词索引
while (start < n) {
int end = start; // 当前行的结束单词索引
int lineLength = 0; // 当前行的字符总长度
// 找到当前行可以容纳的最多单词
while (end < n && lineLength + words[end].length() + (end - start) <= maxWidth) {
lineLength += words[end].length();
end++;
}
int numWords = end - start; // 当前行的单词数量
int numSpaces = maxWidth - lineLength; // 当前行需要插入的空格总数
StringBuilder sb = new StringBuilder();
// 处理特殊情况只有一个单词或是最后一行
if (numWords == 1 || end == n) {
for (int i = start; i < end; i++) {
sb.append(words[i]);
if (i < end - 1) {
sb.append(" ");
}
}
int remainingSpaces = maxWidth - sb.length();
while (remainingSpaces > 0) {
sb.append(" ");
remainingSpaces--;
}
} else {
int spacesPerWord = numSpaces / (numWords - 1); // 单词间的平均空格数
int extraSpaces = numSpaces % (numWords - 1); // 需要额外添加的空格数
for (int i = start; i < end; i++) {
sb.append(words[i]);
if (i < end - 1) {
int spacesCount = spacesPerWord;
if (extraSpaces > 0) {
spacesCount++;
extraSpaces--;
}
for (int j = 0; j < spacesCount; j++) {
sb.append(" ");
}
}
}
}
result.add(sb.toString());
start = end;
}
return result;
}
}
@Autowired @Autowired
private List<Lifecycle> lifecycleList; private List<Lifecycle> lifecycleList;