添加了五个LeetCode问题的解决方案,优化了代码结构。

LeetCode15_$
This commit is contained in:
whaifree 2024-06-03 10:21:19 +08:00
parent af61bc8332
commit 753858679c
10 changed files with 527 additions and 0 deletions

View File

@ -0,0 +1,43 @@
package cn.whaifree.redo.redo_all_240511;
import cn.whaifree.leetCode.model.ListNode;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/6/2 14:49
* @注释
*/
public class LeetCode142 {
@Test
public void test() {
ListNode node = ListNode.listNodeFromArray(new int[]{1,3,2});
node.next.next.next = node.next;
System.out.println(new Solution().detectCycle(node).val);
}
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
do {
if (fast == null || fast.next == null) {
return null;
}
fast = fast.next.next;
slow = slow.next;
} while (fast != slow);
fast = head;
while (fast != slow) {
fast = fast.next;
slow = slow.next;
}
return fast;
}
}
}

View File

@ -0,0 +1,60 @@
package cn.whaifree.redo.redo_all_240511;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* FIXME 24-6-2
*
* @version 1.0
* @Author whai文海
* @Date 2024/6/2 20:43
* @注释
*/
public class LeetCode15_$ {
@Test
public void test() {
int[] nums = {-1, 0, 1, 2, -1, -4};
Solution solution = new Solution();
List<List<Integer>> lists = solution.threeSum(nums);
System.out.println(lists);
}
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int left = i + 1;
int right = nums.length - 1;
while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
if (sum == 0) {
result.add(Arrays.asList(nums[i], nums[left], nums[right]));
while (left < right && nums[left] == nums[left + 1]) {
left++;
}
while (left < right && nums[right] == nums[right - 1]) {
right--;
}
left++;
right--;
} else if (sum < 0) {
left++;
} else {
right--;
}
}
}
return result;
}
}
}

View File

@ -0,0 +1,52 @@
package cn.whaifree.redo.redo_all_240511;
import org.junit.Test;
import java.util.HashSet;
import java.util.Set;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/6/2 16:25
* @注释
*/
public class LeetCode202 {
@Test
public void test() {
Solution solution = new Solution();
boolean happy = solution.isHappy(19);
System.out.println(happy);
}
class Solution {
/**
* 2 4 16 37 56 61 37 出现循环
* @param n
* @return
*/
public boolean isHappy(int n) {
int tmp = n;
Set<Integer> set = new HashSet<>();
while (tmp != 1) {
set.add(tmp);
tmp = get(tmp);
if (set.contains(tmp)) {
return false;
}
}
return true;
}
public int get(int num) {
int sum = 0;
while (num != 0) {
int retail = num % 10;
num /= 10;
sum += retail * retail;
}
return sum;
}
}
}

View File

@ -0,0 +1,54 @@
package cn.whaifree.redo.redo_all_240511;
import cn.whaifree.leetCode.model.ListNode;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/6/2 16:34
* @注释
*/
public class LeetCode2487 {
@Test
public void test() {
ListNode listNode = ListNode.listNodeFromArray(new int[]{1, 1, 1, 1});
new Solution().removeNodes(listNode).printList();
}
class Solution {
public ListNode removeNodes(ListNode head) {
head = reverse(head);
// 如果左侧会更大前移动
// 如果左侧会更小删除左侧
ListNode tmp = head;
ListNode pre = head;
while (tmp!= null) {
// 找到比pre小的
if (tmp.val < pre.val) {
pre.next = tmp.next;
}else {
pre = tmp;
}
tmp = tmp.next;
}
return reverse(head);
}
public ListNode reverse(ListNode head) {
return reverseList(null, head);
}
public ListNode reverseList(ListNode pre, ListNode after) {
if (after == null) {
return pre;
}
ListNode tmp = after.next;
after.next = pre;
return reverseList(after, tmp);
}
}
}

View File

@ -0,0 +1,30 @@
package cn.whaifree.redo.redo_all_240511;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/6/3 10:36
* @注释
*/
public class LeetCode28 {
@Test
public void test() {
Solution solution = new Solution();
int i = solution.strStr("hello", "ll");
System.out.println(i);
System.out.println(solution.strStr("aaaaa", "bba"));
System.out.println(solution.strStr("a", "a"));
}
class Solution {
public int strStr(String haystack, String needle) {
for (int i = 0; i < haystack.length() - needle.length() + 1; i++) {
if (haystack.substring(i, i + needle.length()).equals(needle)) {
return i;
}
}
return -1;
}
}
}

View File

@ -0,0 +1,64 @@
package cn.whaifree.redo.redo_all_240511;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/6/2 14:37
* @注释
*/
public class LeetCode287 {
@Test
public void test() {
int[] nums = {3, 1, 3, 4, 2};
Solution solution = new Solution();
int i = solution.findDuplicate(nums);
System.out.println(i);
}
class Solution {
/**
* 成环路
*
* 1 3 4 2 2
* 0 1 2 3 4
*
* 0--->1--->3--->2--->4--->2
*
* 3 1 3 4 2
* 0 1 2 3 4
*
* 0->3-->4-->2
*
* 快慢指针
*类似链表操作
* i=0
* next i=nums[i]
* nexr.next i=nums[nums[i]]
*
* @param nums
* @return
*/
public int findDuplicate(int[] nums) {
int fast = 0;
int slow = 0;
do {
fast = nums[nums[fast]];
slow = nums[slow];
} while (fast != slow);
// 找到相交点
fast = 0;
while (fast != slow) {
fast = nums[fast];
slow = nums[slow];
}
return fast;
}
}
}

View File

@ -0,0 +1,63 @@
package cn.whaifree.redo.redo_all_240721;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/21 16:48
* @注释
*/
public class LeetCode151 {
public static void main(String[] args) {
String s = " a c ";
System.out.println(reverseWords(s));
}
public static String reverseWords(String s) {
// 去掉前后的空格
// 去掉中间的空格
String[] split = s.split(" ");
StringBuilder sb = new StringBuilder();
for (int i = split.length - 1; i >= 0; i--) {
if (!split[i].equals(" ") && !split[i].isEmpty()) {
sb.append(split[i]).append(" ");
}
}
String sbString = sb.toString();
if (sbString.length() > 1 && sbString.charAt(sbString.length() - 1) == ' ') {
sbString = sbString.substring(0, sbString.length() - 1);
}
return sbString;
}
class Solution {
/**
* 反转字符串s中的单词顺序
*
* @param s 输入的字符串可能包含前后空格
* @return 返回反转单词顺序后的字符串不包含前后空格
*/
public String reverseWords(String s) {
// 去除前后空白串
s = s.trim();
// 将字符串s按一个或多个空格分割成单词数组
String[] split = s.split(" +");
// 使用StringBuilder来高效地构建反转后的字符串
StringBuilder stringBuilder = new StringBuilder();
// 从数组的末尾开始向前遍历依次添加单词到StringBuilder中并在单词间加上空格
for (int i = split.length - 1; i > 0; i--) {
stringBuilder.append(split[i]).append(" ");
}
// 添加数组的第一个单词即原字符串的第一个单词到StringBuilder末尾
stringBuilder.append(split[0]);
// 返回构建好的反转后的字符串
return stringBuilder.toString();
}
}
}

View File

@ -0,0 +1,42 @@
package cn.whaifree.redo.redo_all_240721;
import cn.whaifree.leetCode.model.ListNode;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/21 20:47
* @注释
*/
public class LeetCode203 {
public static void main(String[] args)
{
removeElements(ListNode.listNodeFromArray(new int[]{1, 2, 6, 3, 4, 5, 6}), 6).printList();
}
public static ListNode removeElements(ListNode node, int val) {
if (node == null) {
return null;
}
ListNode next = removeElements(node.next, val);
if (node.val == val) { // 这个node不能用
return next;
}
node.next = next;
return node;
}
public static ListNode remove(ListNode node, int val) {
if (node == null) {
return null;
}
ListNode next = remove(node.next, val);
if (node.val == val) { // 这个node不能用
return next;
}
node.next = next;
return node;
}
}

View File

@ -0,0 +1,86 @@
package cn.whaifree.redo.redo_all_240721;
import java.util.Arrays;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/21 19:52
* @注释
*/
public class LeetCode209 {
public static void main(String[] args) {
int[] nums = {2,3,1,2,4,3};
int target = 7;
System.out.println(minSubArrayLen2(target,nums));
}
/**
* 子数组
* 双指针
*
* - right 向前跑
* - left 计算是否超过target
* @param target
* @param nums
* @return
*/
public static int minSubArrayLen(int target, int[] nums) {
int left = 0;
int right = 0;
int minLength = Integer.MAX_VALUE;
int tmpSum = 0;
while (right < nums.length) {
while (left < right && tmpSum >= target) {
minLength = Math.min(minLength, right - left);
tmpSum -= nums[left];
left++;
}
tmpSum += nums[right];
right++;
}
while (tmpSum >= target) {
minLength = Math.min(minLength, right - left);
tmpSum -= nums[left];
left++;
}
return minLength == Integer.MAX_VALUE ? 0 : minLength;
}
/**
* 前缀和
*
* 2,3,1,2,4,3 纵坐标
* 0 1 2 3 4 5 横坐标
* 0 2 5 6 8 12 15 // 面积
*
* <img src="http://42.192.130.83:9000/picgo/imgs/image-20240329122057505.png">
* @param target
* @param nums
* @return
*/
public static int minSubArrayLen2(int target, int[] nums) {
int[] prefixSum = new int[nums.length + 1];
for (int i = 1; i < prefixSum.length; i++) {
prefixSum[i] = prefixSum[i - 1] + nums[i - 1];
}
int minLength = Integer.MAX_VALUE;
for (int i = 1; i < prefixSum.length; i++) {
// preSum + target = fill
int fill = prefixSum[i] + target;
int end = Arrays.binarySearch(prefixSum, fill);
if (end < 0) {
end = -end - 1;
}
if (end < prefixSum.length) {
minLength = Math.min(minLength, end - i);
}
}
return minLength == Integer.MAX_VALUE ? 0 : minLength;
}
}

View File

@ -0,0 +1,33 @@
package cn.whaifree.test;
import java.util.*;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/6/26 20:03
* @注释
*/
public class Review {
public static void main(String[] args) {
// 输入数组
// 找到整形数组 超过 整形数组中超过
// 1 1 1 2 3
int[] ints = new int[]{1, 1, 1, 1, 5, 6, 7, 2, 1};
System.out.println(method(ints));
}
public static Set<Integer> method(int[] ints) {
Map<Integer, Integer> map = new HashMap<>();
Set<Integer> res = new HashSet<>();
for (int anInt : ints) {
Integer orDefault = map.getOrDefault(anInt, 0) + 1;
map.put(anInt, orDefault);
if (orDefault > ints.length / 2) {
res.add(anInt);
}
}
return res;
}
}