feat: 添加了LeetCode496, LeetCode503, LeetCode33, LeetCode739的解答

This commit is contained in:
whaifree 2024-08-27 01:02:06 +08:00
parent bd1472ccf2
commit b980f346ba
4 changed files with 178 additions and 0 deletions

View File

@ -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;
}
}
}

View File

@ -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<Integer, Integer> map = new HashMap<>();
Deque<Integer> 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;
}
}
}

View File

@ -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<Integer> 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);
}
}
}

View File

@ -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<Integer> 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;
}
}
}