Skip to content

Commit 776e0d5

Browse files
committed
作业
1 parent bc457be commit 776e0d5

8 files changed

Lines changed: 129 additions & 0 deletions

File tree

Week09/125.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def isPalindrome(self, s: str) -> bool:
3+
res = "".join(ch.lower() for ch in s if ch.isalnum())
4+
return res == res[::-1]

Week09/14.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution1:
2+
def longestCommonPrefix(self, strs: List[str]) -> str:
3+
if not strs:
4+
return ""
5+
s = len(strs)
6+
if s == 1:
7+
return strs[0]
8+
n = len(min(strs))
9+
string = ""
10+
flag = True
11+
for j in range(n):
12+
if flag:
13+
for l in range(1, s):
14+
if strs[l][j] == strs[l-1][j]:
15+
if l == s-1:
16+
string += strs[l][j]
17+
else:
18+
flag = False
19+
break
20+
return string
21+
22+
class Solution2:
23+
def longestCommonPrefix(self, strs: List[str]) -> str:
24+
s = ""
25+
for i in zip(*strs):
26+
if len(set(i)) == 1:
27+
s += i[0]
28+
else:
29+
break
30+
return s
31+
32+
class Solution3:
33+
def longestCommonPrefix(self, strs: List[str]) -> str:
34+
if not strs: return ""
35+
str0 = min(strs)
36+
str1 = max(strs)
37+
for i in range(len(str0)):
38+
if str0[i] != str1[i]:
39+
return str0[:i]
40+
return str0

Week09/344.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution1:
2+
def reverseString(self, s: List[str]) -> None:
3+
"""
4+
Do not return anything, modify s in-place instead.
5+
"""
6+
n = len(s)
7+
mid = n // 2
8+
for i in range(mid):
9+
s[i], s[n-i-1] = s[n-i-1], s[i]
10+
return s
11+
12+
class Solution1:
13+
def reverseString(self, s: List[str]) -> None:
14+
"""
15+
Do not return anything, modify s in-place instead.
16+
"""
17+
return s.reverse()

Week09/541.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def reverseStr(self, s: str, k: int) -> str:
3+
if not s:
4+
return s
5+
a = list(s)
6+
n = len(s)
7+
for i in range(0, n, 2*k):
8+
a[i:i+k] = reversed(a[i:i+k])
9+
return "".join(a)

Week09/58.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution1:
2+
def lengthOfLastWord(self, s: str) -> int:
3+
string = s.split()
4+
return len(string[-1]) if string else 0
5+
6+
class Solution2:
7+
def lengthOfLastWord(self, s: str) -> int:
8+
if not s and len(s) == 0:
9+
return 0
10+
count = 0
11+
for i in s[::-1]:
12+
if i == " ":
13+
if count == 0:
14+
continue
15+
else:
16+
break
17+
else:
18+
count += 1
19+
20+
return count
21+

Week09/680.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def validPalindrome(self, s: str) -> bool:
3+
def check(low, high):
4+
i, j = low, high
5+
while i < j:
6+
if s[i] != s[j]:
7+
return False
8+
i += 1
9+
j -= 1
10+
return True
11+
12+
low, high = 0, len(s)-1
13+
while low < high:
14+
if s[low] == s[high]:
15+
low += 1
16+
high -= 1
17+
else:
18+
return check(low+1, high) or check(low, high-1)
19+
return True

Week09/709.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution1:
2+
def toLowerCase(self, str: str) -> str:
3+
dic = {'A':'a', 'B':'b', 'C':'c', 'D':'d', 'E':'e', 'F':'f',
4+
'G':'g', 'H':'h', 'I':'i', 'J':'j', 'K':'k', 'L':'l',
5+
'M':'m', 'N':'n', 'O':'o','P':'p', 'Q':'q', 'R':'r',
6+
'S':'s', 'T':'t', 'U':'u', 'V':'v', 'W':'w', 'X':'x',
7+
'Y':'y', 'Z':'z'}
8+
9+
return "".join([dic.get(s, s) for s in str])
10+
11+
12+
class Solution2:
13+
def toLowerCase(self, str: str) -> str:
14+
return str.lower()

Week09/746.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def minCostClimbingStairs(self, cost: List[int]) -> int:
3+
for i in range(2, len(cost)):
4+
cost[i] = min(cost[i-1], cost[i-2]) + cost[i]
5+
return min(cost[-1], cost[-2])

0 commit comments

Comments
 (0)