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. ios中safari无痕浏览模式下,localStorage的支持情况

    前言 前阶段,测试提了个bug,在苹果手机中无痕模式下,搜索按钮不好使,无法跳页,同时搜索历史也没有展示(用户搜索历史时使用localStorage存储). 正文 iOS上Sarfari在无痕模式下, ...

  2. 饮冰三年-人工智能-Python-24 Django ORM增删改查

    一:首先使用默认的sqlite3创建表 1:现在在models.py中添加表模型 from django.db import models # Create your models here. cla ...

  3. Python目录:

    Python基础 python书写规范--消去提示波浪线 Python 列表(list) Python字符串 Python字典 Python文件操作 Python函数 Python函数-装饰器 Pyt ...

  4. c++ explicit 构造函数

    代码 #include<iostream> using namespace std; class Example { private: int data; public: Example( ...

  5. Kafka(二)CentOS7.5搭建Kafka2.11-1.1.0集群与简单测试

    一.下载 下载地址: http://kafka.apache.org/downloads.html    我这里下载的是Scala 2.11对应的 kafka_2.11-1.1.0.tgz 二.kaf ...

  6. [NewLife.XCode]高级增删改

    NewLife.XCode是一个有10多年历史的开源数据中间件,支持nfx/netstandard,由新生命团队(2002~2019)开发完成并维护至今,以下简称XCode. 整个系列教程会大量结合示 ...

  7. Python requests--初识接口自动化

    requests模块初级宝典:http://docs.python-requests.org/zh_CN/latest/user/quickstart.htmlrequests模块之葵花宝典:http ...

  8. jQuery 获取不到 kindeditor 内容 的解决方法

    错误写法 :  var content = $('#Content').val(); 正确写法: var content = $(document.getElementsByTagName(" ...

  9. Linux epoll机制

    epoll_create.epoll_ctl.epoll_wait.close 在linux的网络编程中,很长的时间都在使用select来做事件触发.在linux新的内核中,有了一种替换它的机制,就是 ...

  10. 关于UITabBarController的设置(iOS 开发)

    1.设置图片(选中以及未选中) UITabBarItem *TuiJianItem=[[UITabBarItem alloc]initWithTitle:@"我的" image:[ ...