LeetCode 5 最长对称串
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 最长对称串的更多相关文章
- 409. Longest Palindrome 最长对称串
[抄题]: Given a string which consists of lowercase or uppercase letters, find the length of the longes ...
- [刷题] PTA 7-64 最长对称子串
7-64 最长对称子串 我的代码: 1 #include<stdio.h> 2 #include<string.h> 3 #define N 1001 4 5 int main ...
- PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)
1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the ...
- c语言:最长对称子串(3种解决方案)
问题描述: 输入一个字符串,输出该字符串中最大对称子串的长度.例如输入字符串:“avvbeeb”,该字符串中最长的子字符串是“beeb”,长度为4,因而输出为4. 解决方法:中序遍历 一,全遍历的方法 ...
- L2-008 最长对称子串 (25 分) (模拟)
链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805067704549376 题目: 对给定的字符串,本题要求你输出 ...
- L2-008. 最长对称子串(思维题)*
L2-008. 最长对称子串 参考博客 #include <iostream> using namespace std; int main() { string s; getline(ci ...
- pat 团体赛练习题集 L2-008. 最长对称子串
对给定的字符串,本题要求你输出最长对称子串的长度.例如,给定"Is PAT&TAP symmetric?",最长对称子串为"s PAT&TAP s&quo ...
- L2-008. 最长对称子串
L2-008. 最长对称子串 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 对给定的字符串,本题要求你输出最长对称子串的长度. ...
- PAT L2-008 最长对称子串(模拟字符串)
对给定的字符串,本题要求你输出最长对称子串的长度.例如,给定Is PAT&TAP symmetric?,最长对称子串为s PAT&TAP s,于是你应该输出11. 输入格式: 输入在一 ...
随机推荐
- [转] 微信小程序页面间通信的5种方式
微信小程序页面间通的5种方式 PageModel(页面模型)对小程序而言是很重要的一个概念,从app.json中也可以看到,小程序就是由一个个页面组成的. 如上图,这是一个常见结构的小程序:首页是一个 ...
- IDEA 通过Maven创建Spring MVC项目搭建
概述 本篇随笔主要记录内容如下: 1.通过Maven创建基于Spring Framework类库的MVC项目,免去了繁琐的XML配置: 2.在Idea里面配置Tomcat的测试启动项: Maven创建 ...
- Centos系统中彻底删除Mysql数据库
步骤: 1.输入命令查询系统中已安装的mysql. rpm -qa |grep -i mysql 2.逐个卸载mysql. yum remove 系统显示已安装的mysql 比如:yum remove ...
- CSS 实现左侧固定,右侧自适应两栏布局的方法
"左边固定,右边自适应的两栏布局",其中有老生常谈的float方法,BFC方法,也有CSS3的flex布局与grid布局.并非所有的布局都会在开发中使用,但是其中也会涉及一些知识点 ...
- easyui提交form表单接受数据处理、
$('#Form').form('submit', { url:"withdrawal/bankAuthenticate4List.do", onSubmit: function( ...
- 网络编程-Mysql-1、数据库的启动关闭,创建数据库,表等基本操作
启动服务端:sudo service mysql start 关闭服务端:suodo service mysql stop 重启服务端:suodo service mysql restart 连接数据 ...
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Peterhof
A. City Wall 找规律. #include<stdio.h> #include<iostream> #include<string.h> #include ...
- [BZOJ1045][HAOI2008]糖果传递 (环形均分纸牌)
题意 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. 思路 把|s[i]-s[k]|求和即可,s[i]是A的前缀和 s[k]为s数组的中位数时,总值 ...
- IE下CSS3伪类的支持
当css3.0出现以后,着实让我兴奋了好久,因为出现了很多选择器,我们在也不用靠js做复杂判断了.比如:nth-child,很容易就可以判断奇偶对象 “:nth-child(2n)和:nth-chil ...
- HTML5_canvas_图片加载_双缓冲_跳帧闪烁问题
canvas 图片加载 pen.drawImage(ele, showX, showY, imgWidth, imgHeight); ele 将 img 元素 加载到画布上 步骤 1. 创建一个 ...