1
2
3
4
5
6
7
8
9
10
11
12
13
|
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
# xa + xb = target, xb = target - xa, use dictionary {} to store key-value pair
# 用dictionary来存key-value对,如果key= target-nums[i] 在字典中则返回 [dict[key], i]
# 否则,把当前的值 nums[i] 做key 存入字典 dict[nums[i]] = i 用做以后的比较
dict = {}
for i, nums[i] in enumerate(nums):
key = target - nums[i]
if key in dict:
return [dict[key], i]
else:
dict[nums[i]] = i
|
每日一记:
创始人刷题的经验:刷题三千多,很感动人。我翻了一下知乎上关于刷题班的评价,众说纷纭。但是,对于刷题这件事,即使是senior级别面试,也是绕不过去的。那么我们就重新开始刷题和训练system design吧,不要让自己闲下来。闲下来也就是刷微信,刷剧,对于我们的成长并无帮助。
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
| # Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
# Time complexity : O(n+m) ; Space complexity : O(1)
# maintain an unchanging reference to node ahead of the return node.
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
prev_head = ListNode(0)
prev = prev_head
while l1 != None and l2 != None:
if l1.val <= l2.val:
prev.next = l1
l1 = l1.next
else:
prev.next = l2
l2 = l2.next
prev = prev.next
if l1 == None :
prev.next = l2
if l2 == None :
prev.next = l1
return prev_head.next
|
文章作者
Hustbill billyzhang2010@gmail.com
上次更新
2021-04-03