Commit 35565e57 by “安晓东”

第四次作业

parent 9522dc17
#
# 时间复杂度较高 O(N^2) # # 时间复杂度较高 O(N^2)
def numJewelsInStones(J, S): # def numJewelsInStones(J, S):
# count = 0 # # count = 0
# for i in S: # # for i in S:
# if i in J: # # if i in J:
# count+=1 # # count+=1
# print(count) # # print(count)
return sum([i in J for i in S]) # return sum([i in J for i in S])
#
#
print(numJewelsInStones("aA", "aAAbbbb")) # 预期输出 3 # print(numJewelsInStones("aA", "aAAbbbb")) # 预期输出 3
#
#
#
#
# o(n)的版本 # # o(n)的版本
def numJewelsInStones(J, S): # def numJewelsInStones(J, S):
stores = {} # stores = {}
for c in S: # for c in S:
if c in stores: # if c in stores:
stores[c] = stores[c] + 1 # stores[c] = stores[c] + 1
else: # else:
stores[c] = 1 # stores[c] = 1
#
count = 0 # count = 0
for c in J: # for c in J:
if c in stores: # if c in stores:
count += stores[c] # count += stores[c]
print(count) # print(count)
#
#
J = "aA" # J = "aA"
S = "aAAbbbb" # S = "aAAbbbb"
numJewelsInStones(J, S) # numJewelsInStones(J, S)
J = "z" # J = "z"
S = "ZZ" # S = "ZZ"
numJewelsInStones(J, S) # numJewelsInStones(J, S)
import re
s ='1324.231.432.1234,192.168.1.6,10.25.11.8这些信息中,哪些是ip呢?'
ret = re.findall('((?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d))', s)
print(ret)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment