diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T05_5_LongestPalindrome.java b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T05_5_LongestPalindrome.java new file mode 100644 index 0000000..166e4b4 --- /dev/null +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T05_5_LongestPalindrome.java @@ -0,0 +1,58 @@ +package com.markilue.leecode.hot100.second; + +import org.junit.Test; + +/** + *@BelongsProject: Leecode + *@BelongsPackage: com.markilue.leecode.hot100.second + *@Author: markilue + *@CreateTime: 2023-04-17 09:50 + *@Description: + * TODO 力扣5题 最长回文子串: + * 给你一个字符串 s,找到 s 中最长的回文子串。 + * 如果字符串的反序与原始字符串相同,则该字符串称为回文字符串。 + *@Version: 1.0 + */ +public class T05_5_LongestPalindrome { + + @Test + public void test() { + String s = "cbbd"; + System.out.println(longestPalindrome(s)); + } + + + /** + * 思路:动态规划判断一个子串是不是回文,如果是判断他是否更长 + * @param s + * @return + */ + public String longestPalindrome(String s) { + + boolean[][] dp = new boolean[s.length() + 1][s.length() + 1];// + + dp[0][0] = true; + char[] chars = s.toCharArray(); + int start = 0; + int length = 0; + + for (int i = 1; i < dp.length; i++) {//end + for (int j = i; j >= 1; j--) {//start + if (chars[i - 1] == chars[j - 1]) { + if (i <= j + 1) { + dp[i][j] = true; + } else { + dp[i][j] = dp[i - 1][j + 1]; + } + } + if (dp[i][j] && i - j + 1 > length) { + start = j-1; + length = i - j + 1; + } + } + } + + return s.substring(start, start + length); + + } +} diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T09_11_MaxArea.java b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T09_11_MaxArea.java new file mode 100644 index 0000000..c6ce4e1 --- /dev/null +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T09_11_MaxArea.java @@ -0,0 +1,66 @@ +package com.markilue.leecode.hot100.second; + +/** + *@BelongsProject: Leecode + *@BelongsPackage: com.markilue.leecode.hot100.second + *@Author: markilue + *@CreateTime: 2023-04-17 10:09 + *@Description: + * TODO 力扣11 盛最多的水的容器: + * 给定一个长度为 n 的整数数组 height 。有 n 条垂线,第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。 + * 找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。 + * 返回容器可以储存的最大水量。 + *@Version: 1.0 + */ +public class T09_11_MaxArea { + + + /** + * 思路:双指针法——哪边矮走哪一边 + * @param height + * @return + */ + public int maxArea(int[] height) { + int left = 0; + int right = height.length - 1; + int maxArea = 0; + + while (left != right) { + int min = Math.min(height[left], height[right]); + maxArea = Math.max(maxArea, min * (right - left)); + if (height[left] < height[right]) { + left++; + } else { + right--; + } + } + + return maxArea; + } + + + /** + * 思路:双指针法——更快 + * @param height + * @return + */ + public int maxArea1(int[] height) { + int left = 0; + int right = height.length - 1; + int maxArea = 0; + + while (left != right) { + if (height[left] < height[right]) { + int pre = height[left]; + maxArea = Math.max(maxArea, pre * (right - left)); + while (left < right && height[left] <= pre) left++; + } else { + int pre = height[right]; + maxArea = Math.max(maxArea, pre * (right - left)); + while (left < right && height[right] <= pre) right--; + } + } + + return maxArea; + } +} diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T19_32_LongestValidParentheses.java b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T19_32_LongestValidParentheses.java new file mode 100644 index 0000000..d459ffa --- /dev/null +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T19_32_LongestValidParentheses.java @@ -0,0 +1,56 @@ +package com.markilue.leecode.hot100.second; + +import org.junit.Test; + +import java.util.LinkedList; + +/** + *@BelongsProject: Leecode + *@BelongsPackage: com.markilue.leecode.hot100.second + *@Author: markilue + *@CreateTime: 2023-04-17 11:35 + *@Description: + * TODO 力扣32 最长有效括号: + * 给你一个只包含 '(' 和 ')' 的字符串,找出最长有效(格式正确且连续)括号子串的长度。 + *@Version: 1.0 + */ +public class T19_32_LongestValidParentheses { + + @Test + public void test(){ + String s = ")()())"; + System.out.println(longestValidParentheses(s)); + } + + + /** + * 使用stack记录对应入栈的索引 + * @param s + * @return + */ + public int longestValidParentheses(String s) { + + LinkedList stack = new LinkedList<>(); + int length = 0; + stack.push(-1); + + char[] chars = s.toCharArray(); + + for (int i = 0; i < chars.length; i++) { + if (chars[i] == '(') { + stack.push(i); + } else { + stack.pop(); + if (stack.isEmpty()) { + stack.push(i); + }else { + length = Math.max(i-stack.peek() , length); + } + + } + } + + return length; + + } +} diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T23_42_Trap.java b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T23_42_Trap.java new file mode 100644 index 0000000..644d192 --- /dev/null +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T23_42_Trap.java @@ -0,0 +1,56 @@ +package com.markilue.leecode.hot100.second; + +import com.sun.media.sound.RIFFInvalidDataException; +import org.junit.Test; + +/** + *@BelongsProject: Leecode + *@BelongsPackage: com.markilue.leecode.hot100.second + *@Author: markilue + *@CreateTime: 2023-04-17 11:15 + *@Description: + * TODO 力扣42 接雨水: + * 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 + *@Version: 1.0 + */ +public class T23_42_Trap { + + @Test + public void test(){ + int[] height={0,1,0,2,1,0,1,3,2,1,2,1}; + System.out.println(trap(height)); + } + + + /** + * 双指针法:谁低走谁 + * @param height + * @return + */ + public int trap(int[] height) { + + int leftMax = height[0]; + int rightMax = height[height.length - 1]; + int sum = 0; + int left = 0; + int right = height.length - 1; + + while (left < right) { + leftMax = Math.max(leftMax, height[left]); + rightMax = Math.max(rightMax, height[right]); + + if (height[left] < height[right]) { + //左边一定的积水 + sum += Math.min(leftMax, rightMax) - height[left]; + left++; + } else { + sum += Math.min(leftMax, rightMax) - height[right]; + right--; + } + + } + + return sum; + + } +}