This commit is contained in:
fleyx 2024-01-04 18:12:43 +08:00
parent 6657ad1707
commit e89cc79716

View File

@ -1,6 +1,7 @@
package com.fanxb.common;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
@ -11,20 +12,42 @@ import java.util.List;
*/
public class Q68 {
public List<String> fullJustify(String[] words, int maxWidth) {
int length = words.length, int leftWidth = maxWidth;
List<String> lineWord = new ArrayList<>();
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){
if (leftWidth >= words[i].length() + 1) {
//可以放下
}else{
//hao
lineWord.add(words[i]);
leftWidth -= words[i].length() + 1;
} else {
//放不下了需要新开一行
lineList.add(lineWord);
lineWord = new LinkedList<>();
leftWidth = maxWidth;
}
}
}
List<String> res = new ArrayList<>(lineList.size());
for (int i = 0; i < lineList.size() - 1; i++) {
addToRes(res, lineList.get(i), false);
}
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));
} else {
}
}
public static void main(String[] args) {