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. 输入格式: 输入在一 ...
随机推荐
- React-Native采坑总结
1.zIndex 在Android上使用zIndex来控制组件的层级,会遇到元素不显示的问题. 解决方案: 尽量改变组件的顺序,而不用zIndex 尽量不要使用zIndex来控制组件的层级,默认情况下 ...
- 简单检测PHP运行效率脚本
<?php $stratTime = microtime(true); $startMemory = memory_get_usage(); $a = 1; for($i = 1; $i < ...
- python之MySQL MySQLdb 推荐使用姿势,解决中文乱码
0.目录 2.setup(1) 安装步骤,可以顺带安装mysql administrator和mysql query browser(2) 安装完毕,修改 my.ini(3) 重启 mysql 服务: ...
- 普通用户登陆系统显示 -bash-4.1$
如题,今天上午用户登陆系统之后显示-bash-4.1$ 造成这样的原因: 与这个用户有关环境变量没了,有关的文件被删除.也就是用户的家目录下面 .bash_profile .bashrc 被删除. 解 ...
- Centos下安装配置Mongodb3.6
首先更新系统 yum -y update 1.安装Mongodb 编辑Mongodb安装源 vim /etc/yum.repos.d/mongodb-org-3.6.repo 编辑内容如下: [mon ...
- arp_spoof脚本的编写
协议包分析 ARP原理: 在PC1 访问 Server 的流量 要经过网关 也就是 192.168.1.254 欺骗原理:pc1 与 网关 建立 MAC ARP 缓存 我们将KALI 攻击机的 ...
- jvm领域优秀博主
dh5724 不过csdn iteye 博客园信息均被自己删除,只在一些问答中和引用中有部分信息. redcreen 对jvm有深入认知. RednaxelaFX oracle java 编译器开发者 ...
- 动态规划——Split Array Largest Sum
题意大概就是,给定一个包含非负整数的序列nums以及一个整数m,要求把序列nums分成m份,并且要让这m个子序列各自的和的最大值最小(minimize the largest sum among th ...
- Android系统源代码目录结构 “Android源代码”“目录结构”
在讲述Android源码编译的三个步骤之前,将先介绍Android源码目录结构,以便读者理清Android编译系统核心代码在Android源代码的位置. Android源代码顶层目录结构如下所示: ├ ...
- Linux bash基础特性一
命令别名 alias cdnet=”cd /etc/sysconfig/network-scripts” 针对用户的别名: “~/.bashrc” 针对系统的别名:”/etc/bashrc” 重读配置 ...