LeetCode 5 最长对称串

最早时候做这道题的时候还是用Java写的,用的是字符串匹配的思路,一直Time Limit Exceeded。甚至还想过用KMP开优化子串查找。

public class Solution {
public String longestPalindrome(String s) {
String reverseS = new StringBuilder(s).reverse().toString(); String maxMatch = "";
for (int i = 0; i < reverseS.length(); i++) {
String match = maxMatch(s, reverseS, i);
if (match.length() > maxMatch.length()) {
maxMatch = match;
} if (maxMatch.length() > s.length() / 2 + 1) {
break;
}
}
return maxMatch;
} /**
* 在s中查找符合pattern匹配的最长子串
*
* TODO 使用KMP优化
*/
public String maxMatch(String s, String pattern, int pBegin) {
int sBegin = 0;
int sIndex = sBegin;
int pIndex = pBegin; String maxMatch = ""; while (sIndex < s.length() && pIndex < pattern.length()) {
if (s.charAt(sIndex) == pattern.charAt(pIndex)) {
String substring = pattern.substring(pBegin, pIndex + 1);
if (substring.length() > maxMatch.length()
&& (substring.length() == 1 || (pattern.length() - pIndex - 1 == sIndex - substring.length() + 1))) {
maxMatch = substring;
} sIndex++;
pIndex++; } else {
sBegin += 1;
sIndex = sBegin;
pIndex = pBegin;
}
} return maxMatch;
}
}

后来做字符串题多了之后,开始熟悉双指针的方法。所谓对称,其实就是从中间往两边查找,如果都一样就继续;不一样就是匹配失败。

"cbbd" 这种情况没有太好的方法,只好两种都尝试一下。

func longestPalindrome(s string) string {
maxLength := 0
begin := 0
if len(s) < 2 {
return s
}
for i := 0; i < len(s); i++ {
var left, right int
left = i - 1
right = i + 1
begin, maxLength = findMax(s, left, right, begin, maxLength)
if i+2 <= len(s) && s[i] == s[i+1] {
left = i - 1
right = i + 2
begin, maxLength = findMax(s, left, right, begin, maxLength)
} }
return s[begin : begin+maxLength]
} func findMax(s string, left int, right int, begin int, maxLength int) (int, int) {
for left >= 0 && right < len(s) && s[left] == s[right] {
left--
right++
}
if maxLength < right-left-1 {
begin = left + 1
maxLength = right - left - 1
}
return begin, maxLength
}

LeetCode 5 最长对称串的更多相关文章

  1. 409. Longest Palindrome 最长对称串

    [抄题]: Given a string which consists of lowercase or uppercase letters, find the length of the longes ...

  2. [刷题] PTA 7-64 最长对称子串

    7-64 最长对称子串 我的代码: 1 #include<stdio.h> 2 #include<string.h> 3 #define N 1001 4 5 int main ...

  3. PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)

    1040 Longest Symmetric String (25 分)   Given a string, you are supposed to output the length of the ...

  4. c语言:最长对称子串(3种解决方案)

    问题描述: 输入一个字符串,输出该字符串中最大对称子串的长度.例如输入字符串:“avvbeeb”,该字符串中最长的子字符串是“beeb”,长度为4,因而输出为4. 解决方法:中序遍历 一,全遍历的方法 ...

  5. L2-008 最长对称子串 (25 分) (模拟)

    链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805067704549376 题目: 对给定的字符串,本题要求你输出 ...

  6. L2-008. 最长对称子串(思维题)*

    L2-008. 最长对称子串 参考博客 #include <iostream> using namespace std; int main() { string s; getline(ci ...

  7. pat 团体赛练习题集 L2-008. 最长对称子串

    对给定的字符串,本题要求你输出最长对称子串的长度.例如,给定"Is PAT&TAP symmetric?",最长对称子串为"s PAT&TAP s&quo ...

  8. L2-008. 最长对称子串

    L2-008. 最长对称子串 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 对给定的字符串,本题要求你输出最长对称子串的长度. ...

  9. PAT L2-008 最长对称子串(模拟字符串)

    对给定的字符串,本题要求你输出最长对称子串的长度.例如,给定Is PAT&TAP symmetric?,最长对称子串为s PAT&TAP s,于是你应该输出11. 输入格式: 输入在一 ...

随机推荐

  1. ionic 3 常见报错及解决办法

    用ionic 3开发也有一段时间了,现在总结下开发中遇到的报错,以及解决办法: ERROR DOMException: Failed to execute 'setAttribute' on 'Ele ...

  2. Android读写配置2

    上篇文章采用 Properties 读写配置,各种路径错误,要么没有写入权限. 后来查资料,采用另一种方式读写 SharedPreferencesImpl 直接贴代码 公共类 -- 读写 packag ...

  3. MATLAB 2012b license checkout failed

    we offer you two ways to license matlab r2012b: standalone1) choose "install manually without u ...

  4. 向量 dot cross product 点积叉积 几何意义

    向量 dot cross product 点积叉积 几何意义 有向量 a b 点积 a * b = |a| * |b| * cosθ 几何意义: 1. a * b == 0,则 a ⊥ b 2. a ...

  5. NEL程序员专用轻钱包 进入0.01状态了

    这个轻钱包能干什么,现在就能在测试网看个余额,转个帐,调用个合约. 而且功能非常程序员化 你会说是不是没啥用   但是他有非常有用,因为他可以很容易的拼出NEOGUI拼不出来的交易 比如参与ICO交易 ...

  6. 友元(friend)

    1.友元类的关系不能传递和继承 ...待续

  7. fflush()函数:更新缓冲区

    fflush()的作用是用来刷新缓冲区: fflush(stdin)刷新标准输入缓冲区,把输入缓冲区里的东西丢弃:stdin是standard input的缩写,即标准输入,一般是指键盘:标准输入缓冲 ...

  8. XVIII Open Cup named after E.V. Pankratiev. Grand Prix of SPb

    A. Base $i - 1$ Notation 两个性质: $2=1100$ $122=0$ 利用这两条性质实现高精度加法即可. 时间复杂度$O(n)$. #include<stdio.h&g ...

  9. Chrome_查看 webSocket 连接信息

    1.以下代码实现一个webSocket连接,在文本输入框中输入内容,点击发送,通过服务器,返回相同的内容显示在下方. <!DOCTYPE html> <html lang=" ...

  10. __x__(15)0906第三天__超链接

    HTML5 中的新属性. 属性 值 描述 charset char_encoding HTML5 中不支持.规定被链接文档的字符集. coords coordinates HTML5 中不支持.规定链 ...