This commit is contained in:
fleyx 2024-01-08 23:46:48 +08:00
parent 81df394d2c
commit f6d2c9c195
4 changed files with 42 additions and 32 deletions

View File

@ -8,7 +8,7 @@ public class Q151 {
StringBuilder temp = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ' ') {
if (!temp.isEmpty()) {
if (temp.length() > 0) {
stack.push(temp.toString());
temp = new StringBuilder();
}
@ -16,7 +16,7 @@ public class Q151 {
temp.append(s.charAt(i));
}
}
if (!temp.isEmpty()) stack.push(temp.toString());
if (temp.length() > 0) stack.push(temp.toString());
StringBuilder res = new StringBuilder();
res.append(stack.pop());
while (!stack.isEmpty()) res.append(" ").append(stack.pop());

View File

@ -1,6 +1,5 @@
package com.fanxb.common;
import com.sun.tools.jconsole.JConsoleContext;
import java.util.*;

View File

@ -18,7 +18,7 @@ public class Q45 {
maxIndex = Math.max(maxIndex, i+nums[i]);
//走到一步的边界了已经找到下一跳的起点
if(i==end){
steps++;
step++;
end = maxIndex;
}
}

View File

@ -1,6 +1,7 @@
package com.fanxb.common;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
@ -12,44 +13,54 @@ import java.util.List;
*/
public class Q68 {
public List<String> fullJustify(String[] words, int maxWidth) {
int length = words.length, leftWidth = maxWidth;
List<String> lineWord = new LinkedList<>();
List<List<String>> lineList = new LinkedList<>();
for (int i = 0; i < length; i++) {
if (lineWord.isEmpty()) {
lineWord.add(words[i]);
leftWidth -= words[i].length();
} else {
if (leftWidth >= words[i].length() + 1) {
//可以放下
lineWord.add(words[i]);
leftWidth -= words[i].length() + 1;
} else {
//放不下了需要新开一行
lineList.add(lineWord);
lineWord = new LinkedList<>();
leftWidth = maxWidth;
}
List<String> res = new ArrayList<>();
List<String> temp = new ArrayList<>();
int size = words.length, lengthCount = 0, usedWidth = 0;
for (int i = 0; i < size; i++) {
lengthCount += words[i].length() + 1;
if (lengthCount > maxWidth + 1) {
//说明放不下了
res.add(dealOne(temp, maxWidth, usedWidth, false));
temp.clear();
usedWidth = 0;
lengthCount = words[i].length() + 1;
}
temp.add(words[i]);
usedWidth += words[i].length();
}
List<String> res = new ArrayList<>(lineList.size());
for (int i = 0; i < lineList.size() - 1; i++) {
addToRes(res, lineList.get(i), false);
if (!temp.isEmpty()) {
res.add(dealOne(temp, maxWidth, usedWidth, true));
}
addToRes(res, lineList.get(lineList.size() - 1), true);
return res;
}
private void addToRes(List<String> res, List<String> line, boolean lastLine) {
StringBuilder temp = new StringBuilder();
temp.append(line.get(0));
if (lastLine) {
for (int i = 1; i < line.size(); i++) temp.append(' ').append(line.get(i));
private String dealOne(List<String> temp, int maxWidth, int usedWidth, boolean lastOne) {
StringBuilder res = new StringBuilder();
res.append(temp.get(0));
int size = temp.size();
if (size == 1) return res.append(" ".repeat(maxWidth - usedWidth)).toString();
if (lastOne) {
for (int i = 1; i < size; i++) res.append(' ').append(temp.get(i));
return res.append(" ".repeat(maxWidth - res.length())).toString();
} else {
//每个间隙的长度
int everyWidth = (maxWidth - usedWidth) / (size - 1);
//前几个间隙需要加一个空格
int needPlusCount = (maxWidth - usedWidth) % (size - 1);
for (int i = 1; i < size; i++) {
res.append(" ".repeat(everyWidth));
if (needPlusCount > 0) {
res.append(' ');
needPlusCount--;
}
res.append(temp.get(i));
}
}
return res.toString();
}
public static void main(String[] args) {
Q68 q68 = new Q68();
System.out.println(q68.fullJustify(new String[]{"This", "is", "an", "example", "of", "text", "justification."}, 16).toString());
}
}