python listnode(-1)_python leetcode 1

开始刷 leetcode, 简单笔记下自己的答案, 目标十一结束之前搞定所有题目.

提高一个要求, 所有的答案执行效率必须要超过 90% 的 python 答题者.

1. Two Sum.

classSolution(object):deftwoSum(self, nums, target):""":type nums: List[int]

:type target: int

:rtype: List[int]"""tmp=[]for i, j inenumerate(nums):

tmp.append((i, j))for k, item inenumerate(tmp):

l, m=itemfor o, p in tmp[k+1:]:if m + p ==target:return sorted((l, o))

runtime beats: 21.30%

优化1

classSolution(object):deftwoSum(self, nums, target):""":type nums: List[int]

:type target: int

:rtype: List[int]"""

for i, j inenumerate(nums):for k, l in enumerate(nums[i+1:]):if j + l ==target:return (i, k + i + 1)

runtime beats: 21.97%

还是不行, 肯定还有别的办法.

2. Add Two Numbers

#Definition for singly-linked list.#class ListNode(object):#def __init_


版权声明:本文为weixin_39774682原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。