# -*- coding:utf-8 -*-
class ChkExpression:
def chkLegal(self, A):
# write code here
stack = []
for i in A:
if i == '(' or i == '[' or i == '{':
stack.append(i)
elif i == ')' or i == ']' or i == '}':
if len(stack) == 0:
return False
stack.pop()
return len(stack) == 0# -*- coding:utf-8 -*-
class TakeBuses:
def chooseLine(self, stops, period, interval, n, s):
# write code here
mins = float('inf')
for i in range(n):
# 若出发时间是间隔的整数倍则正好赶上,否则为差值
misstime = s % interval[i]
if misstime == 0:
waitcost = 0
else:
waitcost = interval[i] - misstime
# 停站数乘耗时5 + 停站时间乘停站数 + 出发到上车时间
mins = min((stops[i] + 1) * 5 + period[i] * stops[i] + waitcost, mins)
return mins + s# -*- coding:utf-8 -*-
class StringFormat:
def formatString(self, A, n, arg, m):
# write code here
while "%s" in A and len(arg) > 0:
A = A.replace("%s", arg.pop(0), 1)
# 把没有替换完的arg也和原字符串结合
return A + ''.join(arg)# -*- coding:utf-8 -*-
class KeywordDetect:
def containKeyword(self, A, n, keys, m):
# write code here
res = []
for i in range(n):
for j in range(m):
if keys[j] in A[i]:
res.append(i)
break
if not res:
return [-1]
return res# -*- coding:utf-8 -*-
'''
总结了一下,一共就三种情况:
含AB(AB与O是特例)
A和B
普通
'''
class ChkBloodType:
def chkBlood(self, father, mother):
# write code here
if 'AB' in [father, mother]:
ans = ['A', 'AB', 'B']
if 'O' in [father, mother]:
ans.remove('AB')
elif 'A' in [father, mother] and 'B' in [father, mother]:
ans = ['A', 'AB', 'B', 'O']
else:
ans = list({father, mother, 'O'})
return ans# -*- coding:utf-8 -*-
class BinarySearch:
def getPos(self, A, n, val):
# write code here
if n <= 0 or A == None:
return -1
l, r = 0, n - 1
while l < r:
mid = (l + r) / 2
if A[mid] > val:
r -= 1
elif A[mid] < val:
l += 1
# 找到相等的那一项位置,靠右的不取
else:
r = mid
if A[l] == val:
return l
return -1# -*- coding:utf-8 -*-
class FirstRepeat:
def findFirstRepeat(self, A, n):
# write code here
temp = []
for i in A:
if i not in temp:
temp.append(i)
else:
return i# -*- coding:utf-8 -*-
class Coder:
def findCoder(self, A, n):
# write code here
# A = ["i am a coder","Coder Coder","Code"]
# a = ['i am a coder', 'Coder Coder']
a = list(filter(lambda c: "coder" in c.lower(), A))
# b = [1, 2]
b = list(map(lambda c: c.lower().count("coder"), a))
# zip(a, b) = [('i am a coder', 1), ('Coder Coder', 2)]
# c = [('Coder Coder', 2), ('i am a coder', 1)]
c= sorted(zip(a, b), key=lambda c: c[1], reverse=True)
# ['Coder Coder', 'i am a coder']
return list(map(lambda x: x[0], c))
版权声明:本文为qq_41805514原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。