add
This commit is contained in:
parent
81df394d2c
commit
f6d2c9c195
@ -8,7 +8,7 @@ public class Q151 {
|
|||||||
StringBuilder temp = new StringBuilder();
|
StringBuilder temp = new StringBuilder();
|
||||||
for (int i = 0; i < s.length(); i++) {
|
for (int i = 0; i < s.length(); i++) {
|
||||||
if (s.charAt(i) == ' ') {
|
if (s.charAt(i) == ' ') {
|
||||||
if (!temp.isEmpty()) {
|
if (temp.length() > 0) {
|
||||||
stack.push(temp.toString());
|
stack.push(temp.toString());
|
||||||
temp = new StringBuilder();
|
temp = new StringBuilder();
|
||||||
}
|
}
|
||||||
@ -16,7 +16,7 @@ public class Q151 {
|
|||||||
temp.append(s.charAt(i));
|
temp.append(s.charAt(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!temp.isEmpty()) stack.push(temp.toString());
|
if (temp.length() > 0) stack.push(temp.toString());
|
||||||
StringBuilder res = new StringBuilder();
|
StringBuilder res = new StringBuilder();
|
||||||
res.append(stack.pop());
|
res.append(stack.pop());
|
||||||
while (!stack.isEmpty()) res.append(" ").append(stack.pop());
|
while (!stack.isEmpty()) res.append(" ").append(stack.pop());
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package com.fanxb.common;
|
package com.fanxb.common;
|
||||||
|
|
||||||
import com.sun.tools.jconsole.JConsoleContext;
|
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ public class Q45 {
|
|||||||
maxIndex = Math.max(maxIndex, i+nums[i]);
|
maxIndex = Math.max(maxIndex, i+nums[i]);
|
||||||
//走到一步的边界了,已经找到下一跳的起点
|
//走到一步的边界了,已经找到下一跳的起点
|
||||||
if(i==end){
|
if(i==end){
|
||||||
steps++;
|
step++;
|
||||||
end = maxIndex;
|
end = maxIndex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.fanxb.common;
|
package com.fanxb.common;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -12,44 +13,54 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class Q68 {
|
public class Q68 {
|
||||||
public List<String> fullJustify(String[] words, int maxWidth) {
|
public List<String> fullJustify(String[] words, int maxWidth) {
|
||||||
int length = words.length, leftWidth = maxWidth;
|
List<String> res = new ArrayList<>();
|
||||||
List<String> lineWord = new LinkedList<>();
|
List<String> temp = new ArrayList<>();
|
||||||
List<List<String>> lineList = new LinkedList<>();
|
int size = words.length, lengthCount = 0, usedWidth = 0;
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
if (lineWord.isEmpty()) {
|
lengthCount += words[i].length() + 1;
|
||||||
lineWord.add(words[i]);
|
if (lengthCount > maxWidth + 1) {
|
||||||
leftWidth -= words[i].length();
|
//说明放不下了
|
||||||
} else {
|
res.add(dealOne(temp, maxWidth, usedWidth, false));
|
||||||
if (leftWidth >= words[i].length() + 1) {
|
temp.clear();
|
||||||
//可以放下
|
usedWidth = 0;
|
||||||
lineWord.add(words[i]);
|
lengthCount = words[i].length() + 1;
|
||||||
leftWidth -= words[i].length() + 1;
|
|
||||||
} else {
|
|
||||||
//放不下了,需要新开一行
|
|
||||||
lineList.add(lineWord);
|
|
||||||
lineWord = new LinkedList<>();
|
|
||||||
leftWidth = maxWidth;
|
|
||||||
}
|
}
|
||||||
|
temp.add(words[i]);
|
||||||
|
usedWidth += words[i].length();
|
||||||
}
|
}
|
||||||
|
if (!temp.isEmpty()) {
|
||||||
|
res.add(dealOne(temp, maxWidth, usedWidth, true));
|
||||||
}
|
}
|
||||||
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;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addToRes(List<String> res, List<String> line, boolean lastLine) {
|
private String dealOne(List<String> temp, int maxWidth, int usedWidth, boolean lastOne) {
|
||||||
StringBuilder temp = new StringBuilder();
|
StringBuilder res = new StringBuilder();
|
||||||
temp.append(line.get(0));
|
res.append(temp.get(0));
|
||||||
if (lastLine) {
|
int size = temp.size();
|
||||||
for (int i = 1; i < line.size(); i++) temp.append(' ').append(line.get(i));
|
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 {
|
} 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) {
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user