B站 9.4 测开笔试
第一题:74%。。。
class Solution: def find_children(self , candiesNeed , candies ): # write code here numsum=0 res=0 visited=set(candies) for num in candies: if candiesNeed-num in visited: i=candies.index(candiesNeed-num) j=candies.index(num) res.append([i,j]) visited.add(num) if not res: return [-1,-1] ans=sorted(res,key=lambda x:(x[0],-x[1])) return ans[0]第二题:
最大公约数
def help2(a,b): a,b=max(a,b),min(a,b) if a%b==0: return b else: return help2(b,a%b) def help1(L): cur=0 for i in range(len(L)): if i ==0: cur=L[i] else: cur=help2(cur,L[i]) return cur print(help1(L))
第三题:打印菱形矩阵
m=n-1 path=[] res=[] ans=[] result='' cur='' i=1 if n%2==0: return '' for _ in range(n//2+1): for _ in range(m//2): path.append('*') for j in range(1,i+1): path.append(str(j)) for j in range(i-1,0,-1): path.append(str(j)) for _ in range(m//2): path.append('*') i+=1 m=m//2 res.append(path[::]) path=[] print(res) for i in range(len(res)): ans.append(res[i]) for i in range(n//2-1,-1,-1): ans.append(res[i]) for i in range(len(ans)): if i==len(ans)-1: cur=''.join(ans[i]) result=result+cur else: cur=''.join(ans[i]) result=result+cur+'|' print(result)