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

LeetCode15_$
This commit is contained in:
whaifree 2024-06-03 10:21:19 +08:00
parent af61bc8332
commit 70816b427e
5 changed files with 273 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,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;
}
}
}