diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode33.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode33.java new file mode 100644 index 0000000..94fdf74 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode33.java @@ -0,0 +1,33 @@ +package cn.whaifree.redo.redoAll; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/27 0:05 + * @注释 + */ +public class LeetCode33 { + + class Solution { + /** + * MA k MB + * [leftA,.... ,leftB] [rightA,.....,rightB] + * + * if nums[mid] > target + * left = MA + * else + * right = + * + * + * + * @param nums + * @param target + * @return + */ + public int search(int[] nums, int target) { + + + return 1; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode496.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode496.java new file mode 100644 index 0000000..2ca5e12 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode496.java @@ -0,0 +1,51 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.Deque; +import java.util.HashMap; +import java.util.LinkedList; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/26 23:30 + * @注释 + */ +public class LeetCode496 { + + @Test + public void test() { + int[] ints = new Solution().nextGreaterElement(new int[]{4,1,2}, new int[]{1,3,4,2}); + for (int anInt : ints) { + System.out.println(anInt); + } + } + + class Solution { + public int[] nextGreaterElement(int[] nums1, int[] nums2) { + + HashMap map = new HashMap<>(); + + Deque stack = new LinkedList<>(); + int[] res = new int[nums2.length]; + Arrays.fill(res, -1); + for (int i = 0; i < nums2.length; i++) { + map.put(nums2[i], i); + while (!stack.isEmpty() && nums2[stack.peek()] < nums2[i]) { + res[stack.peek()] = nums2[i]; + stack.pop(); + } + stack.push(i); + } + + int[] result = new int[nums1.length]; + for (int i = 0; i < nums1.length; i++) { + result[i] = res[map.get(nums1[i])]; + } + + return result; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode503.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode503.java new file mode 100644 index 0000000..2957bd7 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode503.java @@ -0,0 +1,50 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.Deque; +import java.util.LinkedList; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/26 23:45 + * @注释 + */ +public class LeetCode503 { + + @Test + public void test() { + int[] ints = new Solution().nextGreaterElements(new int[]{1,2,1}); + for (int anInt : ints) { + System.out.println(anInt); + + } + + } + + + class Solution { + public int[] nextGreaterElements(int[] nums) { + int[] newNums = new int[nums.length * 2]; + for (int i = 0; i < newNums.length; i++) { + newNums[i] = nums[i % nums.length]; + } + + int[] res = new int[nums.length * 2]; + Deque stack = new LinkedList<>(); + Arrays.fill(res, -1); + for (int i = 0; i < newNums.length; i++) { + while (!stack.isEmpty() && newNums[stack.peek()] < newNums[i]) { + res[stack.peek()] = newNums[i]; + stack.pop(); + } + stack.push(i); + } + + return Arrays.copyOf(res, nums.length); + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode739.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode739.java new file mode 100644 index 0000000..ab404e6 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode739.java @@ -0,0 +1,44 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +import java.util.Deque; +import java.util.LinkedList; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/26 22:57 + * @注释 + */ +public class LeetCode739 { + + @Test + public void test() { + int[] temperatures = {30,40,50,60}; + int[] ints = new Solution().dailyTemperatures(temperatures); + for (int anInt : ints) { + System.out.println(anInt); + } + } + + + class Solution { + public int[] dailyTemperatures(int[] temperatures) { + Deque stack = new LinkedList<>(); + stack.push(0); + + int[] res = new int[temperatures.length]; + for (int i = 1; i < temperatures.length; i++) { + Integer peek = stack.peek(); + while (!stack.isEmpty() && temperatures[peek] < temperatures[i]) { + res[peek] = i - peek; + stack.pop(); + peek = stack.peek(); + } + stack.push(i); + } + return res; + } + } +}