leecode更新

This commit is contained in:
markilue 2023-04-17 13:41:47 +08:00
parent 8eb1d7b2ac
commit eb39bc1bcc
4 changed files with 236 additions and 0 deletions

View File

@ -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];//<start,end>
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);
}
}

View File

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

View File

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

View File

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