<feat> add problem 5 · ananan/leetcode_python@72ced9a · GitHub
Skip to content

Commit 72ced9a

Browse files
committed
<feat> add problem 5
Author:rapospectre@gmail.com Desc: add problem Longest Palindromic Substring
1 parent 6b3fd18 commit 72ced9a

3 files changed

Lines changed: 87 additions & 1 deletion

File tree

readme.md

Lines changed: 6 additions & 1 deletion
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# coding: utf-8
2+
3+
# author: RaPoSpectre
4+
# time: 2016-10-31
5+
6+
7+
# 将每个字符串当作回文串的中心进行验证
8+
9+
class Solution(object):
10+
def find_from_center(self, s, ms, cs):
11+
jts = ''
12+
ots = ''
13+
j = True
14+
o = True
15+
for i in xrange(0, ms + 1):
16+
if s[cs - i] == s[cs + i] and j:
17+
jts = s[cs - i: cs + i + 1]
18+
else:
19+
j = False
20+
if ms + cs + 1 == len(s) and i == ms:
21+
break
22+
if s[cs - i] == s[cs + i + 1] and o:
23+
ots = s[cs - i: cs + i + 2]
24+
else:
25+
o = False
26+
if len(ots) > len(jts):
27+
return ots
28+
return jts
29+
30+
def longestPalindrome(self, s):
31+
"""
32+
:type s: str
33+
:rtype: str
34+
"""
35+
rs = ''
36+
for i in xrange(0, len(s)):
37+
ts = i
38+
te = len(s) - i - 1
39+
if ts < te:
40+
tmp = self.find_from_center(s, ts, i)
41+
else:
42+
tmp = self.find_from_center(s, te, i)
43+
if len(tmp) > len(rs):
44+
rs = tmp
45+
return rs

src/sunday.py

Lines changed: 36 additions & 0 deletions

0 commit comments

Comments
 (0)