feat: 添加了LeetCode题目求解代码

This commit is contained in:
whaifree 2024-09-04 00:46:31 +08:00
parent 6720c74fc3
commit a10c05498c
6 changed files with 399 additions and 0 deletions

View File

@ -0,0 +1,79 @@
package cn.whaifree.redo.redoAll;
import org.junit.Test;
import java.util.Arrays;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/3 22:41
* @注释
*/
public class LeetCode300 {
@Test
public void test() {
int[] nums = {10,9,2,5,3,7,101,18};
Solution1 solution = new Solution1();
int i = solution.lengthOfLIS(nums);
System.out.println(i);
}
class Solution {
public int lengthOfLIS(int[] nums) {
int[] dp = new int[nums.length];
Arrays.fill(dp, 1);
/**
* **dp[i]表示i之前包括i的以nums[i]结尾(每次都需要使用nums[j]来同nums[i]比较)的最长递增子序列的长度**
* 子序列 确定某个数让前面不断与他比较
*/
for (int i = 1; i < nums.length; i++) {
for (int j = i; j >= 0; j--) {
if (nums[j] < nums[i]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}
return Arrays.stream(dp).max().getAsInt();
}
}
class Solution1 {
/**
* dp[i] 表示0-i已经出现的最长
*
* [10,9,2,5,3,7,101,18]
* 1 1 1 2 2 3 4 4
*
* @param nums
* @return
*/
public int lengthOfLIS(int[] nums) {
int[] dp = new int[nums.length];
Arrays.fill(dp, 1);
for (int i = 1; i < nums.length; i++) {
// 不断往后搜索如果比当前值小就有可能是更长的值
int index = i - 1;
while (index >= 0) {
if (nums[i] > nums[index]) {
dp[i] = Math.max(dp[index] + 1, dp[i]);
}
index--;
}
}
return Arrays.stream(dp).max().getAsInt();
}
}
}

View File

@ -0,0 +1,45 @@
package cn.whaifree.redo.redoAll;
import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/2 21:00
* @注释
*/
public class LeetCode337_1 {
@Test
public void test() {
TreeNode root = TreeNode.constructTreeByArray(3, 4, 5, 1, 3, null, 1);
System.out.println(new Solution().rob(root));
}
class Solution {
public int rob(TreeNode root) {
int[] circle = circle(root);
return Math.max(circle[0], circle[1]);
}
/**
*
* @param root
* @return int[0] 为不抢该节点获得最大利润int[1]为抢获得最大利润
*/
public int[] circle(TreeNode root) {
if (root == null) {
return new int[2];
}
int[] left = circle(root.left);
int[] right = circle(root.right);
int a = Math.max(left[0], left[1]) + Math.max(right[0], right[1]); // 不抢这个节点
int b = left[0] + right[0] + root.val; // 抢这个节点
return new int[]{a, b};
}
}
}

View File

@ -0,0 +1,75 @@
package cn.whaifree.redo.redoAll;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/3 23:49
* @注释
*/
public class LeetCode392 {
@Test
public void test() {
String s = "aaaaaa";
String t = "bbaaaa";
Solution1 solution = new Solution1();
boolean subsequence = solution.isSubsequence(s, t);
System.out.println(subsequence);
}
class Solution {
public boolean isSubsequence(String s, String t) {
char[] sCharArray = s.toCharArray();
char[] tCharArray = t.toCharArray();
int index = 0;
for (int i = 0; i < sCharArray.length; i++) {
boolean find = false;
while (index < tCharArray.length) {
if (sCharArray[i] == tCharArray[index]) {
find = true;
index++;
break;
}else {
index++;
}
}
if (!find) {
return false;
}
}
return true;
}
}
class Solution1 {
/**
* dp[i][j] 表示s的i在t的j中出现的长度
*
* "abc" "ahbgdc"
* a h b q d c
* 0
* a 0 1 1 1 1 1 1 相等取上+1
* b 0 0 0 2
* c 0
* @param s
* @param t
* @return
*/
public boolean isSubsequence(String s, String t) {
int[][] dp = new int[s.length() + 1][t.length() + 1];
for (int i = 1; i <= s.length(); i++) {
for (int j = 1; j <= t.length(); j++) {
if (s.charAt(i - 1) == t.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = dp[i][j - 1];
}
}
}
return dp[s.length()][t.length()] == s.length();
}
}
}

View File

@ -0,0 +1,61 @@
package cn.whaifree.redo.redoAll;
import org.junit.Test;
import java.util.Arrays;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/4 0:09
* @注释
*/
public class LeetCode674 {
@Test
public void test() {
int[] nums = {1,3,5,4,7};
System.out.println(new Solution1().findLengthOfLCIS(nums));
}
class Solution {
public int findLengthOfLCIS(int[] nums) {
int max = 0;
int left = 0;
int right = 0;
while (right < nums.length - 1) {
if (nums[right] < nums[right + 1]) {
right++;
}else {
max = Math.max(max, right - left + 1);
right++;
left = right;
}
}
return Math.max(max, right - left + 1);
}
}
class Solution1 {
/**
* dp[i] 表示i目前的最长连续最长递增长度
* 1,3,5,4,7
* 1 2 3 1 2
* @param nums
* @return
*/
public int findLengthOfLCIS(int[] nums) {
int[] dp = new int[nums.length];
dp[0] = 1;
for (int i = 1; i < nums.length; i++) {
if (nums[i] > nums[i - 1]) {
dp[i] = dp[i - 1] + 1;
}else {
dp[i] = 1;
}
}
return Arrays.stream(dp).max().getAsInt();
}
}
}

View File

@ -0,0 +1,39 @@
package cn.whaifree.redo.redoAll;
import cn.whaifree.leetCode.model.ListNode;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/4 0:24
* @注释
*/
public class LeetCode82 {
@Test
public void test() {
new Solution().deleteDuplicates(ListNode.listNodeFromArray(new int[]{1,2,3,3,4,4,5,5})).printList();
}
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode pre = new ListNode(Integer.MAX_VALUE, head);
ListNode preIndex = pre;
ListNode index = pre.next;
while (index!= null) {
if (index.next != null && index.next.val == index.val) {
while (index.next != null && index.next.val == index.val) {
index = index.next;
}
preIndex.next = index.next;
index = preIndex.next;
} else {
preIndex = index;
index = index.next;
}
}
return pre.next;
}
}
}

View File

@ -0,0 +1,100 @@
package cn.whaifree.redo.redoAll;
import org.junit.Test;
import java.util.Deque;
import java.util.LinkedList;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/2 22:32
* @注释
*/
public class LeetCode84_1 {
@Test
public void test() {
int[] heights = {2,1,5,6,2,3};
System.out.println(new Solution().largestRectangleArea(heights));
}
class Solution {
public int largestRectangleArea(int[] heights) {
// 每个元素找到左右第一个比他小的元素
int[] leftMin = new int[heights.length];
int[] rightMin = new int[heights.length];
leftMin[0] = -1;
for (int i = 1; i < heights.length; i++) {
int nowHei = heights[i];
int index = i - 1;
while (index >= 0 && heights[index] >= nowHei) {
index = leftMin[index];
}
leftMin[i] = index;
}
rightMin[heights.length - 1] = heights.length;
for (int i = heights.length - 2; i >= 0; i--) {
int nowHei = heights[i];
int index = i + 1;
while (index <= heights.length - 1 &&heights[index] >= nowHei) {
index = rightMin[index];
}
rightMin[i] = index;
}
int max = 0;
for (int i = 0; i < rightMin.length; i++) {
max = Math.max(max, heights[i] * (rightMin[i] - leftMin[i] - 1));
}
return max;
}
}
class Solution1 {
/**
* 单调栈
*
* 有凹凸 \_/这种 每次弹出就可以计算一次值
*
* @param heights
* @return
*/
public int largestRectangleArea(int[] heights) {
int[] ints = new int[heights.length + 2];
System.arraycopy(heights, 0, ints, 1, heights.length);
heights = ints;
Deque<Integer> stack = new LinkedList<>();
stack.push(0);
int max = 0;
for (int i = 1; i < heights.length; i++) {
int now = heights[i];
if (heights[stack.peek()] > now) {
while (!stack.isEmpty() && now < heights[stack.peek()]) {
/**
*
*
* 2 3 1
*
*/
Integer pop = stack.pop();
if (!stack.isEmpty()) {
max = Math.max(max, (i - stack.peek() - 1) * heights[pop]);
}
}
}
stack.push(i);
}
return max;
}
}
}