diff --git a/pom.xml b/pom.xml index 70b765a..c53e654 100644 --- a/pom.xml +++ b/pom.xml @@ -4,9 +4,10 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - org.example - LeetCode - 1.0-SNAPSHOT + org.example + LeetCode + 1.0-SNAPSHOT + jar UTF-8 17 @@ -36,4 +37,44 @@ + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.10.4 + + + org.apache.maven.plugins + maven-compiler-plugin + + + + org.apache.maven.plugins + maven-resources-plugin + 2.6 + + + + copy-resources + validate + + copy-resources + + + ${project.build.directory}/classes/ + + + src/main/resources + true + + + + + + + + + diff --git a/src/main/java/cn/whaifree/leetCode/LeetCode/LeetCode496.java b/src/main/java/cn/whaifree/leetCode/LeetCode/LeetCode496.java new file mode 100644 index 0000000..a9348c6 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/LeetCode/LeetCode496.java @@ -0,0 +1,113 @@ +package cn.whaifree.leetCode.LeetCode; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.Deque; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.concurrent.ConcurrentHashMap; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/4/9 10:50 + * @注释 + */ +public class LeetCode496 { + + @Test + public void test() { + + ConcurrentHashMap c = new ConcurrentHashMap<>(); + c.put("1", "1"); + + int[] nums1 = {4, 1, 2}; + int[] nums2 = {1, 3, 4, 2}; + int[] res = new Solution1().nextGreaterElement(nums1, nums2); + for (int i : res) { + System.out.println(i); + } + } + + + + + class Solution { + public int[] nextGreaterElement(int[] nums1, int[] nums2) { + // 栈从上到下肯定是递增的 + + // nums2 判断是否比stack1中的栈顶大,如果不是就进入栈2,否则 + + int[] res = new int[nums1.length]; + Arrays.fill(res, -1); + + HashMap map = new HashMap<>(); + for (int i = 0; i < nums1.length; i++) { + map.put(nums1[i], i); + } + + Deque stack = new LinkedList<>(); + stack.push(0); + + for (int i = 1; i < nums2.length; i++) { + if (nums2[i] < nums2[stack.peek()]) { + // 小于 入栈 + stack.push(i); + } else if (nums2[i] > nums2[stack.peek()]) { + // 大于,则至少stack.peek找到了结果 + // 判断nums1中是否存在 + // 即栈顶元素找到了 nums2中第一个大于的元素 + while (!stack.isEmpty() && nums2[i] > nums2[stack.peek()]) { + if (map.containsKey(nums2[stack.peek()])) { + Integer loc = map.get(nums2[stack.peek()]); + res[loc] = nums2[i]; + } + stack.pop(); + } + stack.push(i); + }else { + // 小于 入栈 + stack.push(i); + } + } + return res; + } + } + + class Solution1 { + public int[] nextGreaterElement(int[] nums1, int[] nums2) { + // nums1 中数字 x 的 下一个更大元素 是指 x 在 nums2 中对应位置 右侧 的 第一个 比 x 大的元素。 + + HashMap map = new HashMap<>(); + Deque stack = new LinkedList<>(); + stack.push(0); + for (int i = 1; i < nums2.length; i++) { + if (nums2[stack.peek()] < nums2[i]) { + while (!stack.isEmpty() && nums2[stack.peek()] < nums2[i]) { + int key = nums2[stack.pop()]; + int value = nums2[i]; + map.put(key, value); + } + stack.push(i); + }else if (nums2[stack.peek()] > nums2[i]) { + stack.push(i); + }else { + stack.push(i); + } + } + + int[] res = new int[nums1.length]; + for (int i = 0; i < nums1.length; i++) { + if (map.containsKey(nums1[i])) { + res[i] = map.get(nums1[i]); + }else { + res[i] = -1; + } + } + return res; + } + } + + +} diff --git a/src/main/java/cn/whaifree/leetCode/LeetCode/LeetCode503.java b/src/main/java/cn/whaifree/leetCode/LeetCode/LeetCode503.java new file mode 100644 index 0000000..235f7e6 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/LeetCode/LeetCode503.java @@ -0,0 +1,10 @@ +package cn.whaifree.leetCode.LeetCode; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/4/11 13:07 + * @注释 + */ +public class LeetCode503 { +}