LeetCode题解之Longest Palindromic Substring
1、题目描述
2、问题分析
计算每个字符所组成的字符串的回文子串。
3、代码
string longestPalindrome(string s) { int maxL = ;
int index = ;
bool is_odd = false ;
string res ;
if( s.size() == ){
return res;
} for( int i = ; i < s.size() ; i++){
int n_odd = countLenOfPra(s ,i, i);
int n_even = countLenOfPra(s, i ,i+); if(n_odd > maxL ){
maxL = n_odd;
index = i;
is_odd = true;
}
if(n_even > maxL){
maxL = n_even;
index = i;
is_odd = false;
}
} if( is_odd ){
res = subPralid(s, index , index);
}else{
res = subPralid(s, index , index+);
}
return res;
} int countLenOfPra(string s ,int l ,int r){ while( l >= && r < s.size() && s[l] == s[r]){
l--;
r++;
}
int len = r-l+; return len;
} string subPralid(string s ,int l , int r){
string rs ;
while(l >= && r < s.size() && s[l] == s[r]){
l--;
r++;
}
l += ;
r -= ;
rs = s.substr(l,r-l+);
return rs;
}
LeetCode题解之Longest Palindromic Substring的更多相关文章
- leetcode题解 5. Longest Palindromic Substring
题目: Given a string s, find the longest palindromic substring in s. You may assume that the maximum l ...
- 【LeetCode】5. Longest Palindromic Substring 最长回文子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...
- 【一天一道LeetCode】#5 Longest Palindromic Substring
一天一道LeetCode系列 (一)题目 Given a string S, find the longest palindromic substring in S. You may assume t ...
- 【LeetCode OJ】Longest Palindromic Substring
题目链接:https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...
- 【LeetCode】005. 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 l ...
- 【leetcode】5. Longest Palindromic Substring
题目描述: Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- 《LeetBook》leetcode题解(5):Longest Palindromic [M]——回文串判断
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode OJ:Longest Palindromic Substring(最长的回文字串)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
随机推荐
- Android入门学习总结
1.Manifest.xml是程序运行时读取的文件,是核心的配置文件:也是从中读取Activity 2.主要的代码文件存放在MainActivity.java,里面固定会有onCreate函数会通过s ...
- Word在转PDF的过程中如何创建标签快速方便阅读(图文详解)
不多说,直接上干货! 选择如下 成功! 欢迎大家,加入我的微信公众号:大数据躺过的坑 人工智能躺过的坑 同时,大家可以关注我的个人博客: http://www.cnbl ...
- 全网最详细的Xshell或SecureCRT下spark-shell里出现无法退格或者删除的问题现象的解决办法(图文详解)
不多说,直接上干货! 前言 打开spark的命令行后,发现输错字符了,但是无法退格或者删除,这是比较苦恼的问题. 这个问题,得看你是用Xshell,还是SecureCRT. 一般是出现在SecureC ...
- 最常用的两种C++序列化方案的使用心得(protobuf和boost serialization)
导读 1. 什么是序列化? 2. 为什么要序列化?好处在哪里? 3. C++对象序列化的四种方法 4. 最常用的两种序列化方案使用心得 正文 1. 什么是序列化? 程序员在编写应用程序的时候往往需要将 ...
- 散列算法-SHA
一种生成信息摘要的算法.主要用于数据一致性和完整性的校验 SHA算法分很多版本,最大的分类是SHA-1和SHA-2.SHA-2包括很多子版本,SHA-224,SHA-256,SHA-384,SHA-5 ...
- a no-risk path to IEEE P1687
You’ve heard all about IJTAG (IEEE P1687) [1,2,3], a new standard for accessing embedded test and d ...
- 【IT笔试面试题整理】二叉搜索树转换为双向链表
[试题描述] 将二叉搜索树转换为双向链表 对于二叉搜索树,可以将其转换为双向链表,其中,节点的左子树指针在链表中指向前一个节点,右子树指针在链表中指向后一个节点. 思路一: 采用递归思想,对于二叉搜索 ...
- SQL分区表示例
-- Create tablecreate table TT_FVP_OCR_ADDRESS( id NUMBER not null, waybill_no VARCHAR2(32) not null ...
- spring AOP 之三:使用@AspectJ定义切入点
@AspectJ相关文章 <spring AOP 之二:@AspectJ注解的3种配置> <spring AOP 之三:使用@AspectJ定义切入点> <spring ...
- JS pop push unshift shift的作用与区别
白话JS中数组方法pop push unshift shift的作用与区别,通过本文,你大概能知道这四种数组方法的基本使用与大致区别. 首先,这四种方法会直接修改数组,请先记住这一点. 我们先把pop ...