From 742d0471b2de8381b882875d97b60ded6cfd2c81 Mon Sep 17 00:00:00 2001 From: whai Date: Sun, 7 Jan 2024 21:58:15 +0800 Subject: [PATCH] -- --- .../whaifree/leetCode/String/LeetCode151.java | 21 ++++-- .../java/cn/whaifree/redo/LeetCode151.java | 71 ++++++++++++++++++ .../java/cn/whaifree/redo/LeetCode18.java | 74 +++++++++++++++++++ .../java/cn/whaifree/redo/LeetCode209.java | 40 ++++++++++ .../java/cn/whaifree/redo/LeetCode28.java | 31 ++++++++ 5 files changed, 232 insertions(+), 5 deletions(-) create mode 100644 src/main/java/cn/whaifree/redo/LeetCode151.java create mode 100644 src/main/java/cn/whaifree/redo/LeetCode18.java create mode 100644 src/main/java/cn/whaifree/redo/LeetCode209.java create mode 100644 src/main/java/cn/whaifree/redo/LeetCode28.java diff --git a/src/main/java/cn/whaifree/leetCode/String/LeetCode151.java b/src/main/java/cn/whaifree/leetCode/String/LeetCode151.java index 1462b21..d08ff27 100644 --- a/src/main/java/cn/whaifree/leetCode/String/LeetCode151.java +++ b/src/main/java/cn/whaifree/leetCode/String/LeetCode151.java @@ -2,10 +2,7 @@ package cn.whaifree.leetCode.String; import org.junit.Test; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; +import java.util.*; public class LeetCode151 { @@ -15,7 +12,7 @@ public class LeetCode151 { @Test public void test() { String s = " a b cd ef g "; - System.out.println(new Solution1().reverseWords(s)); + System.out.println(new Solution2().reverseWords(s)); } @@ -109,4 +106,18 @@ public class LeetCode151 { } + + class Solution2{ + + public String reverseWords(String s) { + // 删除前后空白 + String trim = s.trim(); + String[] split = trim.split("\\s+"); + List list = Arrays.asList(split); + Collections.reverse(list); + return String.join(" ", list); + } + + } + } diff --git a/src/main/java/cn/whaifree/redo/LeetCode151.java b/src/main/java/cn/whaifree/redo/LeetCode151.java new file mode 100644 index 0000000..b8fb877 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/LeetCode151.java @@ -0,0 +1,71 @@ +package cn.whaifree.redo; + +import org.junit.Test; + +public class LeetCode151 { + + @Test + public void test() { + Solution solution = new Solution(); + String s = solution.reverseWords(" abc def "); + + System.out.println(s); + } + + class Solution { + public String reverseWords(String s) { + + // 去除空格 + StringBuilder stringBuilder = removeExtraSpace(s); + // 逆转 + reverse(stringBuilder, 0, stringBuilder.length() - 1); + // 对单词逆转 + reverseWord(stringBuilder); + return stringBuilder.toString(); + } + + + public StringBuilder removeExtraSpace(String s) { + int left = 0; + int right = s.length() - 1; + while (s.charAt(left)==' ') left++; + while (s.charAt(right)==' ') right--; + + StringBuilder stringBuilder = new StringBuilder(); + while (left <= right) { + if (s.charAt(left) != ' ' || s.charAt(left + 1) != ' ') { + stringBuilder.append(s.charAt(left)); + } + left++; + } + return stringBuilder; + } + + public void reverse(StringBuilder s, int left, int right) { + while (left < right) { + char tmp = s.charAt(left); + s.setCharAt(left, s.charAt(right)); + s.setCharAt(right, tmp); + left++; + right--; + } + + } + + + public void reverseWord(StringBuilder s) { + + int left = 0; + int right = 0; + while (right <= s.length()) { + if (right == s.length() - 1 || s.charAt(right + 1) == ' ') { + reverse(s, left, right); + left = right++ + 2; + } + right++; + } + + + } + } +} diff --git a/src/main/java/cn/whaifree/redo/LeetCode18.java b/src/main/java/cn/whaifree/redo/LeetCode18.java new file mode 100644 index 0000000..bfff447 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/LeetCode18.java @@ -0,0 +1,74 @@ +package cn.whaifree.redo; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class LeetCode18 { + + @Test + public void test() { +// System.out.println(new Solution().fourSum(new int[]{1, 0, -1, 0, -2, 2}, 0)); + int[] nums = new int[]{-3,-2,-1,0,0,1,2,3}; + // [-3,0,1,2] + System.out.println(new Solution().fourSum(nums, 0)); + } + + // 四个数之和 + class Solution { + public List> fourSum(int[] nums, int target) { + + Arrays.sort(nums); + List> lists = new ArrayList<>(); + + if (nums[0] > target && nums[0] > 0) { + return lists; + } + + for (int i = 0; i < nums.length; i++) { + + // 第一次给过 + if (i > 0 && nums[i] == nums[i - 1]) { + continue; + } + + + for (int j = i + 1; j < nums.length; j++) { + + // 第一次给过 + if (j > i + 1 && nums[j] == nums[j - 1]) { + continue; + } + + int rignt = nums.length - 1; + int left = j + 1; + while (left < rignt) { + int sum = nums[i] + nums[j] + nums[left] + nums[rignt]; + if (sum > target) { + rignt--; + } else if (sum < target) { + left++; + } else { + lists.add(Arrays.asList(nums[i], nums[j], nums[left], nums[rignt])); + + while (left < rignt && nums[left + 1] == nums[left]) left++; + // 注意这里 right-1 + while (left < rignt && nums[rignt - 1] == nums[rignt]) rignt--; + + left++; + rignt--; + + } + + } + + + } + + } + return lists; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/LeetCode209.java b/src/main/java/cn/whaifree/redo/LeetCode209.java new file mode 100644 index 0000000..9e3c6ca --- /dev/null +++ b/src/main/java/cn/whaifree/redo/LeetCode209.java @@ -0,0 +1,40 @@ +package cn.whaifree.redo; + +import org.junit.Test; + +public class LeetCode209 { + + @Test + public void test() { + System.out.println(new Solution().minSubArrayLen(7, new int[]{2,3,1,2,4,3})); + } + + class Solution { + public int minSubArrayLen(int target, int[] nums) { + + + int left = 0; + int right = 0; + int min = Integer.MAX_VALUE; + int sum = 0; + + while (right < nums.length) { + sum += nums[right]; + + while (sum >= target){ + // sum内删除left + sum -= nums[left]; + min = Math.min(min, right - left + 1); + left++; + } + right++; + } + + + + return min == Integer.MAX_VALUE ? 0 : min; + } + + + } +} diff --git a/src/main/java/cn/whaifree/redo/LeetCode28.java b/src/main/java/cn/whaifree/redo/LeetCode28.java new file mode 100644 index 0000000..e59c8a9 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/LeetCode28.java @@ -0,0 +1,31 @@ +package cn.whaifree.redo; + +import org.junit.Test; + +public class LeetCode28 { + + @Test + public void test() { + System.out.println(new Solution().strStr("hello", "h")); + } + + class Solution { + public int strStr(String haystack, String needle) { + + + int begin = 0; + int end = begin + needle.length(); + while (end <= haystack.length()) { + if (haystack.substring(begin, end).equals(needle)) { + return begin; + } + begin++; + end++; + } + + + + return -1; + } + } +}