From f6d2c9c195c7ae4bb829877e8bc87820a94e0527 Mon Sep 17 00:00:00 2001 From: fleyx Date: Mon, 8 Jan 2024 23:46:48 +0800 Subject: [PATCH] add --- 5.leetcode/src/com/fanxb/common/Q151.java | 4 +- 5.leetcode/src/com/fanxb/common/Q401.java | 1 - 5.leetcode/src/com/fanxb/common/Q45.java | 2 +- 5.leetcode/src/com/fanxb/common/Q68.java | 67 +++++++++++++---------- 4 files changed, 42 insertions(+), 32 deletions(-) diff --git a/5.leetcode/src/com/fanxb/common/Q151.java b/5.leetcode/src/com/fanxb/common/Q151.java index 2623c3e..8fcc19a 100644 --- a/5.leetcode/src/com/fanxb/common/Q151.java +++ b/5.leetcode/src/com/fanxb/common/Q151.java @@ -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()); diff --git a/5.leetcode/src/com/fanxb/common/Q401.java b/5.leetcode/src/com/fanxb/common/Q401.java index 5b78a0b..ad90b6d 100644 --- a/5.leetcode/src/com/fanxb/common/Q401.java +++ b/5.leetcode/src/com/fanxb/common/Q401.java @@ -1,6 +1,5 @@ package com.fanxb.common; -import com.sun.tools.jconsole.JConsoleContext; import java.util.*; diff --git a/5.leetcode/src/com/fanxb/common/Q45.java b/5.leetcode/src/com/fanxb/common/Q45.java index 8fdaf52..97a365b 100644 --- a/5.leetcode/src/com/fanxb/common/Q45.java +++ b/5.leetcode/src/com/fanxb/common/Q45.java @@ -18,7 +18,7 @@ public class Q45 { maxIndex = Math.max(maxIndex, i+nums[i]); //走到一步的边界了,已经找到下一跳的起点 if(i==end){ - steps++; + step++; end = maxIndex; } } diff --git a/5.leetcode/src/com/fanxb/common/Q68.java b/5.leetcode/src/com/fanxb/common/Q68.java index b475ca9..2df2943 100644 --- a/5.leetcode/src/com/fanxb/common/Q68.java +++ b/5.leetcode/src/com/fanxb/common/Q68.java @@ -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 fullJustify(String[] words, int maxWidth) { - int length = words.length, leftWidth = maxWidth; - List lineWord = new LinkedList<>(); - List> 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 res = new ArrayList<>(); + List 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 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 res, List 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 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()); } }