Update files and add new classes LeetCode496, LeetCode503, and others, with related tests and solutions.
This commit is contained in:
parent
040afd5488
commit
0ec6ab1e6c
47
pom.xml
47
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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.example</groupId>
|
||||
<artifactId>LeetCode</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<groupId>org.example</groupId> <!--groupId表示当前 Maven 项目隶属的组织或公司-->
|
||||
<artifactId>LeetCode</artifactId> <!--当前 Maven 项目的名称-->
|
||||
<version>1.0-SNAPSHOT</version><!--定义了 Maven 项目当前所处版本-->
|
||||
<packaging>jar</packaging> <!--定义了 Maven 项目的打包方式(比如 jar,war...),默认使用 jar。-->
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
@ -36,4 +37,44 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<!--javadoc-->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>2.10.4</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
<!--maven-resource-plugin-->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>2.6</version>
|
||||
<!-- 'resources', 'outputDirectory'-->
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-resources</id>
|
||||
<phase>validate</phase>
|
||||
<goals>
|
||||
<goal>copy-resources</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/classes/</outputDirectory>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
|
113
src/main/java/cn/whaifree/leetCode/LeetCode/LeetCode496.java
Normal file
113
src/main/java/cn/whaifree/leetCode/LeetCode/LeetCode496.java
Normal file
@ -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<Object, Object> 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<Integer, Integer> map = new HashMap<>();
|
||||
for (int i = 0; i < nums1.length; i++) {
|
||||
map.put(nums1[i], i);
|
||||
}
|
||||
|
||||
Deque<Integer> 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<Integer, Integer> map = new HashMap<>();
|
||||
Deque<Integer> 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
10
src/main/java/cn/whaifree/leetCode/LeetCode/LeetCode503.java
Normal file
10
src/main/java/cn/whaifree/leetCode/LeetCode/LeetCode503.java
Normal file
@ -0,0 +1,10 @@
|
||||
package cn.whaifree.leetCode.LeetCode;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/4/11 13:07
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode503 {
|
||||
}
|
Loading…
Reference in New Issue
Block a user