[leetcode] 5. Longest Palindromic Substring (Medium)
找到并返回最长回路子串
思路:
解法一:
最简单的双重遍历,判断s[i]到s[j]是不是回串。
Runtime: 610 ms, faster than 6.39% of Java 慢的不行
class Solution {
public String longestPalindrome(String s) {
int len=s.length();
for (int i = 0; i < len; i++) {
int subNum = i + 1;
int subLen = len - i;
for (int j = 0; j < subNum; j++) {
String subStr = s.substring(j, j + subLen);
if (isPalindrome(subStr))
return subStr;
}
}
return "";
}
public boolean isPalindrome(String s){
int beg = 0, end = s.length() - 1;
while(beg<end){
if(s.charAt(beg)!=s.charAt(end))
return false;
beg++;
end--;
}
return true;
}
}
解法二:
遍历一次,以每一个s[i]为中心,计算。
Runtime: 4 ms, faster than 100.00% of Java
class Solution {
int len = 0, maxLength = 0, start = 0;
public String longestPalindrome(String s) {
char[] arr = s.toCharArray();
len = s.length();
if (len <= 1)
return s;
for (int i = 0; i < len; i++) {
i = helper(arr, i);
}
return s.substring(start, start + maxLength);
}
public int helper(char[] arr, int k) {
int i = k - 1, j = k;
while (j < len - 1 && arr[j] == arr[j + 1])
j++;
int nextCenter = j++;
while (i >= 0 && j < len && arr[i] == arr[j]) {
i--;
j++;
}
if (j - i - 1 > maxLength) {
maxLength = j - i - 1;
start = i + 1;
}
return nextCenter;
}
}
[leetcode] 5. Longest Palindromic Substring (Medium)的更多相关文章
- LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法
LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之 ...
- Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)
Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...
- 蜗牛慢慢爬 LeetCode 5.Longest Palindromic Substring [Difficulty: Medium]
题目 Given a string s, find the longest palindromic substring in s. You may assume that the maximum le ...
- 求最长回文子串 - leetcode 5. Longest Palindromic Substring
写在前面:忍不住吐槽几句今天上海的天气,次奥,鞋子里都能养鱼了...裤子也全湿了,衣服也全湿了,关键是这天气还打空调,只能瑟瑟发抖祈祷不要感冒了.... 前后切了一百零几道leetcode的题(sol ...
- LeetCode 5 Longest Palindromic Substring(最长子序列)
题目来源:https://leetcode.com/problems/longest-palindromic-substring/ Given a string S, find the longest ...
- 【JAVA、C++】LeetCode 005 Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
- [LeetCode][Python]Longest Palindromic Substring
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- 【LeetCode】Longest Palindromic Substring 解题报告
DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...
随机推荐
- 全面解析ECMAScript 6模块系统
快速使用Romanysoft LAB的技术实现 HTML 开发Mac OS App,并销售到苹果应用商店中. <HTML开发Mac OS App 视频教程> 土豆网同步更新:http: ...
- C++中代理类和句柄类
指针是 C 与其他语言区别的重要特征之一,在 C++ 中,指针也被广泛运用,我们通过指针实现多态.然而,众所周知,指针的使用必须小心,否则很容易造成内存泄漏 Memory Leak.当我们有几个指针指 ...
- How Qt Signals and Slots Work(感觉是通过Meta根据名字来调用)
Qt is well known for its signals and slots mechanism. But how does it work? In this blog post, we wi ...
- centos安装最新版MySQL 8.0教程
这篇教程是通过yum方式安装的 安装依赖 yum install libaio wget -y 检查MYSQL是否已安装 yum list installed | grep mysql 如果有先卸载 ...
- 【练习题】proj1 判断二叉树子树和是否为指定的值
#include <stdio.h> #include <vector> #include <list> #include<iostream> usin ...
- Fabric1.4源码解析:客户端创建通道过程
在使用Fabric创建通道的时候,通常我们执行一条命令完成,这篇文章就解析一下执行这条命令后Fabric源码中执行的流程. peer channel create -o orderer.example ...
- java源码解析之String类(三)
上一节我们主要讲了String类的一些不是很常用的方法,其中需要掌握的如下,我就不再赘述了 public int length() public boolean isEmpty() public by ...
- git push 时:报missing Change-Id in commit message footer的错误
1. 一般而言,按照提示执行以下两个命令即可生成新的Change-id - gitdir=$(git rev-parse --git-dir); scp -p -P 29418 guan@192.16 ...
- Tomcat之端口占用问题的解决
我们在使用Tomcat的时候经常会遇到端口占用的问题,如下图所示: 那么怎么解决这个问题呢? 第一步,你得知道什么占据了8080.8005.8009端口: 按win+R,输入cmd,打开命令行窗口,在 ...
- Linux 配置 history 命令显示操作时间、用户和登录 IP
一.在配置文件中(/etc/bashrc 或者 /etc/profile 或者~/.bash_profile 或者 ~/.bashrc)添加如下配置 #vim /etc/bashrc // 进到 ...