08 13 哔哩哔哩 笔试 后端开发 Python
分析
30道选择题,3道编程题105分钟,编程题简单,重点还是选择
1
给定四个数值,判断能不能经过加减乘除运算得到24.
- 回溯,中间遇到的坑:不能直接对遍历的操作使用eval,有运算符的优先级问题
# # # @param arr int整型一维数组 # @return bool布尔型 # class Solution: def Game24Points(self , arr ): # write code here visited = [0] * 4 def helper(visited,s): if sum(visited) == 4: ss = s.copy() res = ss.pop(0) #计算结果是不是24 while ss: res = eval(str(res)+ss.pop(0)) if res == 24: return True return False #回溯 for i in range(len(arr)): if not visited[i]: for symbol in ['+','-','*','/']: s.append(symbol+str(arr[i])) visited[i] = 1 if helper(visited,s): return True visited[i] = 0 s.pop() return False for i in range(len(arr)): visited[i] = 1 if helper(visited,[str(arr[i])]): return True visited[i] = 0 return False a = Solution() print(a.Game24Points([7,2,1,10]))
2.
字符串匹配 easy题
class Solution: def IsValidExp(self , s ): # write code here q_map = {')':'(',']':'[','}':'{'} if not s:return True stack = [] for i in range(len(s)): if s[i] not in q_map.keys(): stack.append(s[i]) elif not stack:return False elif q_map[s[i]] != stack.pop(): return False if not stack:return True return False a = Solution() print(a.IsValidExp('{{}}}'))
3
换零钱,贪心。easy题
class Solution: def GetCoinCount(self , N ): # write code here vals = [64,16,4,1] money = 1024 - N res = 0 for val in vals: cur_cnt = money // val money -= cur_cnt * val res += cur_cnt return res
总结
拼选择题和学历的时候到了[Dog]
全部评论
(6) 回帖