2015年6月25日

继续刷题,Merge two sorted lists这题,Python暴力排序和判断排序居然是一样的时间……

https://leetcode.com/problems/merge-two-sorted-lists/

先试了试判断排序:

def mergeTwoLists(self, l1, l2):
l = []

while l1 and l2:
if l1.val <= l2.val:
l.append(l1.val)
l1 = l1.next
else:
l.append(l2.val)
l2 = l2.next

while l1:
l.append(l1.val)
l1 = l1.next

while l2:
l.append(l2.val)
l2 = l2.next

return l

然后72ms

然后无聊想看看暴力读取然后排序:

def mergeTwoLists(self, l1, l2):
l = []

while l1:
l.append(l1.val)
l1 = l1.next

while l2:
l.append(l2.val)
l2 = l2.next

l.sort()

return l

居然还是72ms……

没有评论: