This commit is contained in:
whai 2024-01-06 15:40:09 +08:00
parent 929adbf6bb
commit bd0902dfc4
4 changed files with 215 additions and 0 deletions

View File

@ -0,0 +1,59 @@
package cn.whaifree.redo;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class LeetCode15 {
@Test
public void test() {
int[] nums = {1,0,0};
List<List<Integer>> lists = new Solution().threeSum(nums);
System.out.println(lists);
}
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> lists = new ArrayList<>();
// 计算三个数的和
for (int i = 0; i < nums.length; i++) {
//
if (i != 0 && nums[i - 1] == nums[i]) { // 第一次不进入循环i!=0
continue;
}
int left = i + 1;
int right = nums.length - 1;
while (left < right) {
int sum = nums[left] + nums[right] + nums[i];
if (sum > 0) {
right--;
} else if (sum < 0) {
left++;
} else {
lists.add(Arrays.asList(nums[left], nums[right], nums[i]));
left++;
right--;
while (left < right && nums[left] == nums[left - 1]) { // 去重且避免数组都是一个数字的情况
left++;
}
while (left < right && nums[right] == nums[right + 1]) { // 去重且避免数组都是一个数字的情况
right--;
}
}
}
}
return lists;
}
}
}

View File

@ -0,0 +1,43 @@
package cn.whaifree.redo;
import org.junit.Test;
import java.util.HashSet;
import java.util.Set;
public class LeetCode202 {
@Test
public void test() {
System.out.println(new Solution().isHappy(2));
}
class Solution {
/**
* 无限循环
* @param n
* @return
*/
public boolean isHappy(int n) {
Set<Integer> set = new HashSet<>();
while (n != 1) {
if (set.contains(n)) {
return false;
}
set.add(n);
n = get(n);
}
return true;
}
public int get(int n) {
int sum = 0;
while (n != 0) {
int e = n % 10;
sum += e * e;
n /= 10;
}
return sum;
}
}
}

View File

@ -0,0 +1,62 @@
package cn.whaifree.redo;
import cn.whaifree.leetCode.model.ListNode;
import org.junit.Test;
public class LeetCode2487 {
@Test
public void test() {
ListNode head = ListNode.listNodeFromArray(new int[]{5, 2, 13, 3, 8});
ListNode reverse = new Solution().removeNodes(head);
reverse.printList();
}
/**
*
* 移除每个右侧有一个更大数值的节点
* [5,2,13,3,8]
* 输出[13,8]
*
* - 递减的
*
*
*
*/
class Solution {
public ListNode removeNodes(ListNode head) {
// 逆转 8 3 13 2 5
head = reverse(head);
// 删除全部比当前点小的值
ListNode index = head;
ListNode maxNode = head;
while (index.next != null) {
if (maxNode.val > index.next.val) {
index.next = index.next.next;
} else {
index = index.next;
maxNode = index;
}
}
return head;
}
// 1 2 3 4 5
public ListNode reverse(ListNode head) {
ListNode pre = null;
ListNode index = head;
ListNode tmp = null;
while (index != null) {
tmp = index.next;
index.next = pre;
pre = index;
index = tmp;
}
return pre;
}
}
}

View File

@ -0,0 +1,51 @@
package cn.whaifree.redo;
import cn.hutool.core.lang.hash.Hash;
import org.junit.Test;
import java.util.HashSet;
import java.util.Set;
public class LeetCode287 {
@Test
public void test() {
System.out.println(new Solution().findDuplicate(new int[]{1, 3, 4, 3, 2}));
}
class Solution {
/**
* 要求 ON的时间复杂度
* O1的控空间
*
* 将数组转换为链表的思想即求有没有形成环
* 1 3 4 2 2
* 0 1 2 3 4
* 0-->1-->2-->4--->2
*
* @param nums
* @return
*/
public int findDuplicate(int[] nums) {
// 1. 找到相遇的点
// 2. 将快指针指向首部
// 3. 两个指针重合处就是
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;
}
}
}