[LeetCode]-algorithms-Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000,
and there exists one unique longest palindromic substring.
要求:给定一个字符串,求最长的回文字符串
"a"
=>a
"runcodes"
=>r
"runcodestatus"
=>tat
"runcodestats"
=>stats
public String longestPalindrome(String s) {
s = s.trim();
if(null==s || "".equals(s))
return null;
String res = s.substring(0,1);
String temp;
for(int i=0; i<s.length()-1; i++){
temp = search(s, i, i);
if(temp.length()>res.length())
res = temp;
temp = search(s, i, i+1);
if(temp.length()>res.length())
res = temp;
}
return res;
}
//这个方法向 i 的两端发散
public String search(String s,int begin,int end){
while(begin>=0 && end<s.length() && (s.charAt(begin)==s.charAt(end))){
begin--;
end++;
}
return s.substring((begin+1),end);
}
分析:以每个字符为中心,遍历前后的字符串
[LeetCode]-algorithms-Longest Palindromic Substring的更多相关文章
- 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
写在前面:忍不住吐槽几句今天上海的天气,次奥,鞋子里都能养鱼了...裤子也全湿了,衣服也全湿了,关键是这天气还打空调,只能瑟瑟发抖祈祷不要感冒了.... 前后切了一百零几道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 ...
- [LeetCode] 5. Longest Palindromic Substring 最长回文子串
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
随机推荐
- 洛谷 P3368 树状数组 题解
题面 本题随便看两眼就知道该题满足了优美的查分性质: 对于在区间[x,y]内操作时,应该将查分数组的第x项和第y+1项进行相反操作: 询问答案时,问第i个数的值就是查分数组的前i项和: 暴力+玄学卡常 ...
- java按值传递?
原文链接:https://blog.csdn.net/scholar_man/article/details/80900212 在Java中,参数都是按值传递的.被传递到方法中的拷贝值,要不就是一个引 ...
- python 链接mysql 连接池
# python 链接mysqlimport mysql.connector.poolingconfig = { "host":"localhost", &qu ...
- KL散度的理解(GAN网络的优化)
原文地址Count Bayesie 这篇文章是博客Count Bayesie上的文章Kullback-Leibler Divergence Explained 的学习笔记,原文对 KL散度 的概念诠释 ...
- linux环境查看版本信息
1.uname -a 2.cat /proc/version 3.cat /etc/issue 4.cat /proc/cpuinfo (查看cpu相关信息)
- vue学习【四】vuex快速入门
大家好,我是一叶,今天我们继续踩坑.今天的内容是vuex快速入门,页面传值不多的话,不建议vuex,直接props进行父子间传值就行,使用vuex就显得比较臃肿. 我们先预览一下效果,如图1所示. 图 ...
- SQLite 参数化查询
SQLite参数化查询 首先给出两个参考博客: Sqlite DB sqlite3使用简介 贴出一段自己用的代码: #include <stdio.h> #include <stdl ...
- 一、doT.js使用笔记
一.赋值https://www.jianshu.com/p/19156f9fac1e <!DOCTYPE html> <html> <head> <meta ...
- phpstorm配置总结
phpstorm配合laravel框架作为项目开发,需要添加自动提示,减少查看文档的次数,本次使用的是idel-helper插件 在当前项目下 编辑composer.json文件文件,添加如下字符 & ...
- 使用kong-dashboard
具体命令行操作只需按照官网一步一步来即可(https://docs.konghq.com/1.4.x/getting-started/configuring-a-service/),这里介绍图形化操作 ...