update dp
This commit is contained in:
@@ -193,7 +193,7 @@ int longest_common_subsequence(const char *s1,const char *s2, char *common){
|
||||
```
|
||||
|
||||
|
||||
## 对称子字符串的最大长度
|
||||
## 对称子字符串的最大长度 (最长回文子串)
|
||||
|
||||
题目:输入一个字符串,输出该字符串中对称的子字符串的最大长度。比如输入字符串“google”,由于该字符串里最长的对称子字符串是“goog”,因此输出4。
|
||||
|
||||
@@ -215,6 +215,37 @@ int max_symmetrical_char_length(const char *scr){
|
||||
```
|
||||
|
||||
|
||||
```Java
|
||||
public String longestPalindrome(String s) {
|
||||
String res = "";
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
// 以 s[i] 为中心的最长回文子串
|
||||
String s1 = palindrome(s, i, i);
|
||||
|
||||
// 以 s[i] 和 s[i+1] 为中心的最长回文子串
|
||||
String s2 = palindrome(s, i, i + 1);
|
||||
|
||||
// res = longest(res, s1, s2)
|
||||
res = res.length() > s1.length() ? res : s1;
|
||||
res = res.length() > s2.length() ? res : s2;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
//实现一个函数来寻找最长回文串
|
||||
String palindrome(String s, int l, int r) {
|
||||
// 防止索引越界
|
||||
while (l >= 0 && r < s.length()
|
||||
&& s.charAt(l) == s.charAt(r)) {
|
||||
// 向两边展开
|
||||
l--; r++;
|
||||
}
|
||||
// 返回以 s[l] 和 s[r] 为中心的最长回文串
|
||||
return s.substring(l + 1, r);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 子串匹配的个数
|
||||
|
||||
|
||||
@@ -81,7 +81,6 @@ int findaddends(int *data,int length,int sum,int *a,int *b);
|
||||
|
||||
|
||||
|
||||
|
||||
## 求子数组的最大和(最大字段和)
|
||||
|
||||
输入一个整形数组,数组里有正数也有负数。
|
||||
@@ -91,7 +90,7 @@ int findaddends(int *data,int length,int sum,int *a,int *b);
|
||||
例如输入的数组为`1, -2, 3, 10, -4, 7, 2, -5` ,和最大的子数组为`3, 10, -4, 7, 2` ,因此输出为该子数组的和18。
|
||||
|
||||
1. 蛮力法 fmax(i,j) 找出最大的值,3重循环 ,复杂度 0(n^3)
|
||||
2. maxendindhere保存当前累加的和,如果<0,就把maxendinghere清零 , max保存最终的最大和; 如果都是负数
|
||||
2. `动态规划` maxendindhere保存当前累加的和,如果<0, 就把maxendinghere清零 , max保存最终的最大和; 如果都是负数
|
||||
```
|
||||
int maxSumOfVector(int *data,int length){
|
||||
|
||||
|
||||
Reference in New Issue
Block a user