第1題:
####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題
# 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題