最长公共前缀
【给你一个大小为 n 的字符串数组 strs ,其中包含n个字符串 , 编写一个函数来查找字符串数组中的最长公共前缀,返回这个公共前缀。】
class Solution:
def longestCommonPrefix(self , strs: List[str]) -> str:
# write code here
res = ''
for i in zip(*strs):
if len(set(i)) == 1:
res += i[0]
else:
break
return res
大数加法
【以字符串的形式读入两个数字,编写一个函数计算它们的和,以字符串形式返回。】
class Solution:
def solve(self , s: str, t: str) -> str:
# write code here
if len(s) < len(t): s = "0"*(len(t)-len(s)) + s
else: t = "0"*(len(s)-len(t)) + t
s = s[::-1]
t = t[::-1]
flag = 0
result = ""
print(s,t)
for i in range(len(s)):
temp = int(s[i]) + int(t[i]) + flag
if temp >= 10:
result += str(temp%10)
flag = 1
else:
if temp >= 10:
result += str(temp%10)
flag = 1
else:
result += str(temp)
flag = 0
if flag == 1:result += str(flag)
return result[::-1]
验证IP地址
class Solution:
def solve(self , IP: str) -> str:
# write code here
ip4 = IP.split(".")
ip6 = IP.split(":")
print(ip4,ip6)
if len(ip4) != 4 and len(ip6)!=8:
return "Neither"
if len(ip4)==4:
for x in ip4:
if not x.isdigit(): return "Neither"
if int(x) < 0 or int(x)>255:
return "Neither"
if x[0]=="0" and int(x)!=0:
return "Neither"
return "IPv4"
if len(ip6)==8:
for x in ip6:
if len(x)>4 or len(x)==0:
return "Neither"
if not x.isalnum():
return "Neither"
for c in x:
if not (c.isdigit() or c>="a" and c<="f" or c>="A" and c<="F"):
return "Neither"
return "IPv6"