腾讯面试编程题
第1题:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | ####solution1##########case通过率为100% import copy input_l = input ().strip() try : n = int (input_l) res = [] for i in range (n): t = input () res.append( int (t)) res2 = copy.deepcopy(res) minl = res[ 0 ] maxl = res[n - 1 ] j = 0 temp = 0 while j < n - 1 : res.sort() a = res.pop() b = res.pop() res.append(a * b + 1 ) j = j + 1 minl = res[ 0 ] k = 0 while k < n - 1 : res2.sort() a = res2.pop( 0 ) b = res2.pop( 0 ) res2.append(a * b + 1 ) k = k + 1 maxl = res2[ 0 ] print (maxl - minl) except ValueError: print ( "输入的不是数字" ) |
解析:每次将列表中最大两个数进行指定运算后去除,将运算结果加入列表,这样得到最后一个数是最小数
每次将列表中最小的两个数进行指定运算后去除,将运算结果加入列表,这样得到最后一个数是最大数
第2题:
第1题
同 Leetcode的第2题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def addTwoNumbers( self , l1: ListNode, l2: ListNode) - > ListNode: cur = ListNode( 0 ) head = cur temp = 0 a = 0 while l1 and l2: a = l1.val + l2.val + temp temp = 0 if a> = 10 : temp = a / / 10 a = a % 10 node = ListNode(a) cur. next = node l1 = l1. next l2 = l2. next cur = cur. next while l2: a = l2.val + temp temp = 0 if a> = 10 : temp = a / / 10 a = a % 10 node = ListNode(a) cur. next = node l2 = l2. next cur = cur. next while l1: a = l1.val + temp temp = 0 if a> = 10 : temp = a / / 10 a = a % 10 node = ListNode(a) cur. next = node l1 = l1. next cur = cur. next if temp: node = ListNode(temp) cur. next = node cur = cur. next return head. next |
第2题
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步