2024-1月 13 14日复刷

This commit is contained in:
whai 2024-01-14 14:21:17 +08:00
parent 0a10a5dcce
commit 3df5579ab3
20 changed files with 548 additions and 17 deletions

View File

@ -48,7 +48,7 @@ public class LeetCode347 {
System.out.println(i);
}
}
class Solution {

View File

@ -1,4 +1,4 @@
package cn.whaifree.redo;
package cn.whaifree.redo.redo24_1_7;
import org.junit.Test;

View File

@ -1,4 +1,4 @@
package cn.whaifree.redo;
package cn.whaifree.redo.redo24_1_7;
import org.junit.Test;

View File

@ -1,4 +1,4 @@
package cn.whaifree.redo;
package cn.whaifree.redo.redo24_1_7;
import org.junit.Test;

View File

@ -1,4 +1,4 @@
package cn.whaifree.redo;
package cn.whaifree.redo.redo24_1_7;
import org.junit.Test;

View File

@ -1,10 +1,10 @@
package cn.whaifree.redo;
package cn.whaifree.redo.redo24_1_7;
import cn.whaifree.leetCode.model.ListNode;
import org.junit.Test;
public class LeetCode203 {
public class LeetCode203_false {
@Test
public void test(){

View File

@ -1,10 +1,10 @@
package cn.whaifree.redo;
package cn.whaifree.redo.redo24_1_7;
import cn.whaifree.leetCode.model.ListNode;
import org.junit.Test;
// 递归逆转链表
public class LeetCode206 {
public class LeetCode206_false {
@Test
public void test() {

View File

@ -1,8 +1,8 @@
package cn.whaifree.redo;
package cn.whaifree.redo.redo24_1_7;
import org.junit.Test;
public class LeetCode209 {
public class LeetCode209_false {
@Test
public void test() {

View File

@ -1,9 +1,9 @@
package cn.whaifree.redo;
package cn.whaifree.redo.redo24_1_7;
import cn.whaifree.leetCode.model.ListNode;
import org.junit.Test;
public class LeetCode2487 {
public class LeetCode2487_false {
@Test
public void test() {

View File

@ -1,4 +1,4 @@
package cn.whaifree.redo;
package cn.whaifree.redo.redo24_1_7;
import cn.hutool.core.lang.hash.Hash;
import org.junit.Test;
@ -6,7 +6,7 @@ import org.junit.Test;
import java.util.HashSet;
import java.util.Set;
public class LeetCode287 {
public class LeetCode287_false {
@Test
public void test() {

View File

@ -1,8 +1,8 @@
package cn.whaifree.redo;
package cn.whaifree.redo.redo24_1_7;
import org.junit.Test;
public class LeetCode28 {
public class LeetCode28_false {
@Test
public void test() {

View File

@ -0,0 +1,10 @@
package cn.whaifree.redo.redo_24_1_13;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/1/14 14:20
* @注释
*/
public class LeetCode203 {
}

View File

@ -0,0 +1,46 @@
package cn.whaifree.redo.redo_24_1_13;
import org.junit.Test;
import java.util.Arrays;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/1/13 16:25
* @注释
*/
public class LeetCode209 {
@Test
public void test() {
System.out.println(new Solution().minSubArrayLen(41, new int[]{1,1,1,4,4}));
}
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int left = 0;
int right = 0;
int minLength = Integer.MAX_VALUE;
int sum = 0;
do {
if (sum + nums[right] >= target) {
if (right - left + 1 < minLength) {
minLength = right - left + 1;
}
sum -= nums[left];
left++;
} else {
sum += nums[right];
right++;
}
} while (right < nums.length); // 让右指针移动就好了
return minLength == Integer.MAX_VALUE ? 0 : minLength;
}
}
}

View File

@ -0,0 +1,73 @@
package cn.whaifree.redo.redo_24_1_13;
import org.junit.Test;
import java.util.Deque;
import java.util.LinkedList;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/1/13 13:36
* @注释
*/
public class LeetCode225 {
@Test
public void test() {
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.push(3);
myStack.push(4);
myStack.push(5);
myStack.push(6);
myStack.push(7);
myStack.push(8);
myStack.push(9);
myStack.push(10);
System.out.println(myStack.pop());
System.out.println(myStack.top());
System.out.println(myStack.pop());
}
class MyStack {
public Deque<Integer> queue = null;
public MyStack() {
queue = new LinkedList<Integer>();
}
public void push(int x) {
int size = queue.size();
queue.add(x);
for (int i = 0; i < size; i++) {
queue.add(queue.pop());
}
}
public int pop() {
return queue.pop();
}
public int top() {
return queue.peek();
}
public boolean empty() {
return queue.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
}

View File

@ -0,0 +1,70 @@
package cn.whaifree.redo.redo_24_1_13;
import org.junit.Test;
import java.util.Deque;
import java.util.LinkedList;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/1/13 13:48
* @注释
*/
public class LeetCode232 {
@Test
public void test() {
MyQueue myQueue = new MyQueue();
myQueue.push(1);
myQueue.push(2);
myQueue.push(3);
System.out.println(myQueue.peek());
System.out.println(myQueue.pop());
System.out.println(myQueue.peek());
}
class MyQueue {
Deque<Integer> stack1;
Deque<Integer> stack2;
public MyQueue() {
stack1 = new LinkedList<>();
stack2 = new LinkedList<>();
}
public void push(int x) {
// 全部弹出到另一个栈再全部弹回去
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
stack1.push(x);
while (!stack2.isEmpty()) {
stack1.push(stack2.pop());
}
}
public int pop() {
return stack1.pop();
}
public int peek() {
return stack1.peek();
}
public boolean empty() {
return stack1.isEmpty();
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
}

View File

@ -0,0 +1,70 @@
package cn.whaifree.redo.redo_24_1_13;
import org.junit.Test;
import java.util.Deque;
import java.util.LinkedList;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/1/13 13:58
* @注释
*/
public class LeetCode239_false {
@Test
public void test() {
Solution solution = new Solution();
int[] nums = new int[]{1, 3, -1, -3, 5, 3, 6, 7};
int k = 3;
int[] ints = solution.maxSlidingWindow(nums, k);
for (int anInt : ints) {
System.out.println(anInt);
}
}
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
Deque<Integer> queue = new LinkedList<Integer>();
int length = nums.length;
int[] res = new int[length - k + 1];
// 从0-k-1遍历找到最大值
// 存下标到res数组中
for (int i = 0; i < k; i++) {
while (!queue.isEmpty() && nums[i] > nums[queue.peekLast()]) {
queue.removeLast();
}
queue.addLast(i);
}
// 遍历剩余的k-1 --- length-1
// 如果找到比当前优先队列首位大的下标就加入队列
// 并且需要判断当前窗口是不是在正确的区间内
// 第一个元素必然是queue.peek对应的数值
res[0] = nums[queue.peek()];
for (int i = k; i < length; i++) {
while (!queue.isEmpty() && nums[i] > nums[queue.peekLast()]) {
queue.removeLast();
}
queue.addLast(i);
if (queue.peek() <= i - k) {
queue.removeFirst();
}
// 注入res
Integer peek = queue.peek();
res[i - k + 1] = nums[peek];
}
return res;
}
}
}

View File

@ -0,0 +1,59 @@
package cn.whaifree.redo.redo_24_1_13;
import cn.whaifree.leetCode.model.ListNode;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/1/14 12:24
* @注释
*/
public class LeetCode2487 {
@Test
public void test() {
ListNode head = ListNode.listNodeFromArray(new int[]{5, 2, 13, 1, 3, 8});
new Solution().removeNodes(head).printList();
}
class Solution {
public ListNode removeNodes(ListNode head) {
// 逆转
ListNode newHead = reverse(head);
// 移除左边有更小的节点
remove(newHead);
// 再逆转
return reverse(newHead);
}
public void remove(ListNode head) {
ListNode index = head;
ListNode targetNode = head;
while (index.next != null) {
if (index.next.val < targetNode.val) {
index.next = index.next.next;
}else {
index = index.next;
targetNode = index;
}
}
}
public ListNode reverse(ListNode head) {
ListNode pre = null;
ListNode index = head;
while (index != null) {
ListNode tmp = index.next;
index.next = pre;
pre = index;
index = tmp;
}
return pre;
}
}
}

View File

@ -0,0 +1,33 @@
package cn.whaifree.redo.redo_24_1_13;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/1/14 11:26
* @注释
*/
public class LeetCode28 {
@Test
public void test() {
System.out.println(new Solution().strStr("leetcode", "leeto"));
}
class Solution {
public int strStr(String haystack, String needle) {
int length = needle.length();
int index = 0;
int giveLength = haystack.length();
while (index <= giveLength - length) {
if (haystack.substring(index, index + length).equals(needle)) {
return index;
}
index++;
}
return -1;
}
}
}

View File

@ -0,0 +1,60 @@
package cn.whaifree.redo.redo_24_1_13;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/1/14 12:53
* @注释
*/
public class LeetCode287 {
@Test
public void test() {
System.out.println(new Solution().findDuplicate(new int[]{1, 3, 4, 2, 2}));
}
class Solution {
/**
* 寻找循环链表的入口
* 1 3 4 2 2
* 0 1 2 3 4
*
* 0--->1--->3--->2--->4--->2
*
*
*
* @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,110 @@
package cn.whaifree.redo.redo_24_1_13;
import org.junit.Test;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/1/13 15:10
* @注释
*/
public class LeetCode347 {
@Test
public void test() {
int[] x = new Solution1().topKFrequent(new int[]{2, 2, 1, 1, 1, 1, 1, 4, 4, 4, 4, 5, 5, 5, 3, 3, 3, 3, 3, 3}, 2);
for (int i : x) {
System.out.println(i);
}
}
class Solution {
public int[] topKFrequent(int[] nums, int k) {
// 统计频率
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i])) {
map.put(nums[i], map.get(nums[i]) + 1);
} else {
map.put(nums[i], 1);
}
}
// 大顶堆
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(
new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return map.get(o2) - map.get(o1);
}
}
);
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
priorityQueue.add(entry.getKey());
}
int[] ints = new int[k];
for (int i = 0; i < k; i++) {
ints[i] = priorityQueue.poll();
}
return ints;
}
}
class Solution1 {
public int[] topKFrequent(int[] nums, int k) {
// 统计频率
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i])) {
map.put(nums[i], map.get(nums[i]) + 1);
} else {
map.put(nums[i], 1);
}
}
// 小顶堆
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(
new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return map.get(o1) - map.get(o2);
}
}
);
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
// 导入前k个形成初始堆
if (priorityQueue.size() < k) {
priorityQueue.add(entry.getKey());
} else if (map.get(entry.getKey()) > map.get(priorityQueue.peek())) {
// 如果 该元素的频率比最小的值都大则插入并且删除最小值
priorityQueue.remove();
priorityQueue.add(entry.getKey());
}
}
int[] ints = new int[k];
for (int i = 0; i < k; i++) {
ints[i] = priorityQueue.poll();
}
return ints;
}
}
}