由于笔试要求不能向外***,所以仅放代码以供参考。
笔者很惨,第一条应该没问题但是测试没过,第二条由于分析错误导致未能写完,结束后自己编写了测试用例,调试通过了。
1.
2.
def divingBoard(a, b, k): if k == 0:return [] temp = set() for i in range(k+1): s = i*a + (k-i)*b temp.add(s) res = [] while temp: res.append(temp.pop()) return sorted(res)
2.
def setNode(s): t = s.split('|') tree = [] for i in t: node = i.split('^') tree.append(node) return tree def dfs(root,cur,tree,res): if root[2] == 'END': cur += int(root[1]) res.append(cur) cur -= int(root[1]) else: cur += int(root[1]) next = root[2].split(',') for n in next: for i in range(len(tree)): if tree[i][0] == n: index = i dfs(tree[index],cur,tree,res) def treeMaxValue(s): tree = setNode(s) res = [] dfs(tree[0],0,tree,res) return max(res) s = 'HEAD^0^A,B,C|A^50^END|B^20^END|C^30^D,E|D^10^F|E^20^END|F^40^END' print(treeMaxValue(s))
全部评论
(0) 回帖