Skip to content

Commit a0a62bf

Browse files
committed
homework week 09
1 parent 5c0eb94 commit a0a62bf

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

Week_09/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
学习笔记
1+
学习笔记
2+
3+
字符串处理,使用Python代码更容易实现,并且不会有繁琐的索引、类型转换

Week_09/firstUniqChar.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def firstUniqChar(self, s: str) -> int:
3+
counter = {}
4+
if len(s) == 0:
5+
return -1
6+
for c in s:
7+
if c in counter:
8+
counter[c] += 1
9+
else:
10+
counter[c] = 1
11+
12+
for k, v in counter.items():
13+
if v == 1:
14+
return s.index(k)
15+
return -1

Week_09/panlindrome.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
maxLen =0
3+
lowIdx = 0
4+
def extendPalindrome(self, s, j, k):
5+
while j >=0 and k < len(s) and s[j] == s[k] :
6+
j -= 1
7+
k += 1
8+
if k - j -1 > self.maxLen:
9+
self.lowIdx = j+1
10+
self.maxLen = k - j -1
11+
12+
def longestPalindrome(self, s: str) -> str:
13+
if len(s) < 2:
14+
return s
15+
16+
size = len(s)
17+
for i in range(0, size-1):
18+
self.extendPalindrome(s, i, i)
19+
self.extendPalindrome(s, i, i+1)
20+
return s[self.lowIdx:self.lowIdx + self.maxLen]

0 commit comments

Comments
 (0)