leetcode解题—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.
解题:参考网上大神做法,解题如下:
class Solution: def get_palindromic(self, s, k, l):
s_len = len(s)
while k >= 0 and l < s_len and s[k] == s[l]:
k -= 1
l += 1
return s[k+1:l] def longestPalindrome(self, s):
L_palindromic = ''
for i in range(len(s)):
temp_palindromic1 = self.get_palindromic(s, i, i)
if len(temp_palindromic1) > len(L_palindromic):
L_palindromic = temp_palindromic1 temp_palindromic2 = self.get_palindromic(s, i, i+1)
if len(temp_palindromic2) > len(L_palindromic):
L_palindromic = temp_palindromic2 return L_palindromic
leetcode解题—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】Longest Palindromic Substring 解题报告
DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...
- 【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 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 ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
- Java [leetcode 5] Longest Palindromic Substring
问题描述: Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- [LeetCode][Python]Longest Palindromic Substring
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
随机推荐
- MySQL Optimizer Tracemy 与 logmnr:MySQL binlog logmnr----MYSQL 邱伟胜 专家博客
http://www.noodba.com/ http://www.yhddba.com/?tag=mylogmnr https://github.com/noodba
- procps工具集 ----Linux中的可用内存指的是什么?
https://gitlab.com/procps-ng/procps free - Report the amount of free and used memory in the system k ...
- java_泛型(构造器)部分实例
package ming; import java.util.ArrayList; import java.util.Collection; import java.util.List; class ...
- iOS之NSUserDefaults的用法
NSUserDefaults适合存储轻量级的本地数据,比如要保存一个登陆界面的数据,用户名.密码之类的,个人觉得使用NSUserDefaults是首选.下次再登陆的时候就可以直接从NSUserDefa ...
- 深入理解计算机系统第二版习题解答CSAPP 2.15
只使用位级运算和逻辑运算,编写一个C表达式,它等价于x==y.换句话说,当x和y相等时它将返回1,否则就返回0. !(x ^ y)
- UVA - 213 Message Decoding (输入字符串并对单个字符进行操作的输入输出)
POINT: 关于表示一个编码:利用code字符数组表示一个编码字符,其中code[len][val]表示长度为len,二进制值为val的字符: 主程序如下: #include <iostrea ...
- Android Studio中文组(中文社区)
Android Studio中文组(中文社区)http://www.android-studio.org/
- div中的img垂直居中
<html> <head> <style type="text/css"> .imgDiv { overflow: hidden; displa ...
- postgresql 将同一个字段的值组合和将多个字段的值组合
多字段值根据连接符拼接 concat_ws(':',aaa,bbb) 单字段值根据连接符拼接 string_agg(ccc,' \r\n ') 如果要将多个字段的值拼接成一个: string_agg( ...
- EasyUIDataGrid 的List<T>转Json
EasyUI的DataGrid的Json自己拼接的话非常麻烦,而且容易出错,于是写了个通用的方法! CustomList<T>自定义类,继承于List<T>,用来处理返回的实体 ...