8.4 动规+数组
This commit is contained in:
parent
f417d9d716
commit
8b91ec0731
@ -13,8 +13,25 @@ package array;
|
||||
|
||||
* 链接:https://leetcode.cn/problems/remove-duplicates-from-sorted-array/
|
||||
*/
|
||||
//二刷会做
|
||||
public class RemoveDuplicates {
|
||||
//很啰嗦
|
||||
|
||||
/**
|
||||
* 第一轮遍历(标记重复元素)
|
||||
* pre 指针记录上一个有效元素位置
|
||||
* 遇到重复值时:
|
||||
* 将当前元素标记为 invalid(Integer.MIN_VALUE)
|
||||
* deleted 计数器记录删除数量
|
||||
* 非重复时:移动 pre 指针
|
||||
* 第二轮遍历(填充有效元素)
|
||||
* 找到被标记为 invalid 的位置
|
||||
* 从 start 或当前位置向后搜索第一个有效元素
|
||||
* 交换/填充有效值到前面
|
||||
* 返回结果
|
||||
* 最终有效长度 = 原长度 - 删除数量
|
||||
|
||||
*/
|
||||
public int removeDuplicates2(int[] nums) {
|
||||
int pre=0,deleted=0,start=0,invalid=Integer.MIN_VALUE;
|
||||
for (int i = 1; i < nums.length; i++) {
|
||||
|
@ -11,6 +11,7 @@ package array;
|
||||
|
||||
* 链接:https://leetcode.cn/problems/remove-duplicates-from-sorted-array-ii/
|
||||
*/
|
||||
//二刷会做
|
||||
public class RemoveDuplicates2 {
|
||||
//自己写的
|
||||
public int removeDuplicates(int[] nums) {
|
||||
|
@ -15,6 +15,31 @@ package dynamic_programming;
|
||||
*/
|
||||
//二刷会做
|
||||
public class LongestCommonSubsequence {
|
||||
public int longestCommonSubsequence2(String text1, String text2) {
|
||||
int len1=text1.length(),len2=text2.length();
|
||||
int[][]dp=new int[len1+1][len2+1];
|
||||
for (int i = 1; i <= len1; i++) {
|
||||
for (int j = 1; j <= len2; j++) {
|
||||
if(text1.charAt(i-1)==text2.charAt(j-1))
|
||||
dp[i][j]=dp[i-1][j-1]+1;
|
||||
else
|
||||
dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]);
|
||||
}
|
||||
}
|
||||
return dp[len1][len2];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public int longestCommonSubsequence(String text1, String text2) {
|
||||
int n=text1.length(),m=text2.length();
|
||||
int[][]dp=new int[n+1][m+1];
|
||||
|
@ -1,4 +1,7 @@
|
||||
package dynamic_programming;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 题目: 5. 最长回文子串 (longestPalindrome)
|
||||
* 描述:给你一个字符串 s,找到 s 中最长的 回文 子串。
|
||||
@ -11,7 +14,9 @@ package dynamic_programming;
|
||||
|
||||
* 链接:https://leetcode.cn/problems/longest-palindromic-substring/
|
||||
*/
|
||||
//二刷不会
|
||||
public class LongestPalindrome {
|
||||
|
||||
public String longestPalindrome(String s) {
|
||||
int n = s.length();
|
||||
if (n < 2) return s;
|
||||
@ -35,4 +40,51 @@ public class LongestPalindrome {
|
||||
}
|
||||
return s.substring(start, start + maxLen);
|
||||
}
|
||||
public String longestPalindrome2(String s) {
|
||||
int len = s.length();
|
||||
if (len < 2) {
|
||||
return s;
|
||||
}
|
||||
|
||||
int maxLen = 1;
|
||||
int begin = 0;
|
||||
// dp[i][j] 表示 s[i..j] 是否是回文串
|
||||
boolean[][] dp = new boolean[len][len];
|
||||
// 初始化:所有长度为 1 的子串都是回文串
|
||||
for (int i = 0; i < len; i++) {
|
||||
dp[i][i] = true;
|
||||
}
|
||||
|
||||
char[] charArray = s.toCharArray();
|
||||
// 递推开始
|
||||
// 先枚举子串长度
|
||||
for (int L = 2; L <= len; L++) {
|
||||
// 枚举左边界,左边界的上限设置可以宽松一些
|
||||
for (int i = 0; i < len; i++) {
|
||||
// 由 L 和 i 可以确定右边界,即 j - i + 1 = L 得
|
||||
int j = L + i - 1;
|
||||
// 如果右边界越界,就可以退出当前循环
|
||||
if (j >= len) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (charArray[i] != charArray[j]) {
|
||||
dp[i][j] = false;
|
||||
} else {
|
||||
if (j - i < 3) {
|
||||
dp[i][j] = true;
|
||||
} else {
|
||||
dp[i][j] = dp[i + 1][j - 1];
|
||||
}
|
||||
}
|
||||
|
||||
// 只要 dp[i][L] == true 成立,就表示子串 s[i..L] 是回文,此时记录回文长度和起始位置
|
||||
if (dp[i][j] && j - i + 1 > maxLen) {
|
||||
maxLen = j - i + 1;
|
||||
begin = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return s.substring(begin, begin + maxLen);
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,9 @@ package dynamic_programming;
|
||||
* 链接:https://leetcode.cn/problems/uncrossed-lines/
|
||||
*/
|
||||
//转为求最长子序列
|
||||
//二刷会做
|
||||
public class MaxUncrossedLines {
|
||||
|
||||
public int maxUncrossedLines(int[] nums1, int[] nums2) {
|
||||
int len1 = nums1.length;
|
||||
int len2 = nums2.length;
|
||||
|
Loading…
x
Reference in New Issue
Block a user