This commit is contained in:
whai 2024-01-07 21:58:15 +08:00
parent bd0902dfc4
commit 742d0471b2
5 changed files with 232 additions and 5 deletions

View File

@ -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<String> list = Arrays.asList(split);
Collections.reverse(list);
return String.join(" ", list);
}
}
}

View File

@ -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++;
}
}
}
}

View File

@ -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<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
List<List<Integer>> 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;
}
}
}

View File

@ -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;
}
}
}

View File

@ -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;
}
}
}