1.
if hashmap.get(target-num) is not None:
强调!! 看有没有key对应的值 is not None
暴力破解:
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
ans = []
l = len(nums)
for i in range(l):
for j in range(i+1,l):
if nums[j] == target -nums[i]:
ans = [i,j]
return ans
用hashmap:
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hashmap = {}
for i,num in enumerate(nums):
if hashmap.get(target-num) is not None:
return [i,hashmap.get(target-num)]
hashmap[num] = i
注意: 可以用dict表示hashmap
赋值就是dict[i] = num
12.
class Solution:
def intToRoman(self, num: int) -> str:
hashmap = {1000:'M',900:'CM',500:'D',400:'CD',100:'C',90:'XC',50:'L',40:'XL',10:'X',9:'IX',5:'V',4:'IV',1:'I'}
ans = ''
for key in hashmap:
char = hashmap[key]
n = num // key
ans = ans+char*n
num = num % key
return ans
21.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
Head = ListNode(0)
move = Head
while l1 and l2:
if l1.val<=l2.val:
move.next = l1
l1= l1.next
else:
move.next = l2
l2 = l2.next
move = move.next
if l1 and not l2:
move.next = l1
else:
move.next = l2
return Head.next
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
if not l1: return l2
if not l2: return l1
if l1.val <= l2.val:
l1.next = self.mergeTwoLists(l1.next,l2)
return l1
else:
l2.next = self.mergeTwoLists(l1,l2.next)
return l2
23
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeKLists(self, lists: List[ListNode]) -> ListNode:
if not lists:
return
k = len(lists)
Head = lists[0]
for i in range(1,k):
Head = self.merge(Head,lists[i])
return Head
def merge(self,l1,l2):
if not l1:
return l2
if not l2:
return l1
if l1.val <= l2.val:
l1.next = self.merge(l1.next,l2)
return l1
else:
l2.next = self.merge(l1,l2.next)
return l2
分治法:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeKLists(self, lists: List[ListNode]) -> ListNode:
n = len(lists)
if n == 0: return
if n == 1:return lists[0]
mid = n // 2
return self.mergetwolists(self.mergeKLists(lists[:mid]),self.mergeKLists(lists[mid:]))
def mergetwolists(self,l1,l2):
Head = ListNode()
cur = Head
while l1 and l2:
if l1.val <=l2. val:
cur.next = l1
l1 = l1.next
else:
cur.next = l2
l2 = l2.next
cur = cur.next
if not l1:
cur.next = l2
else:
cur.next = l1
return Head.next
用堆来实现排序:
错误代码:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeKLists(self, lists: List[ListNode]) -> ListNode:
Head = ListNode(0)
que = []
cur = Head
for i in range(len(lists)):
heapq.heapqpush(que,(lists[i],i))
while que:
val_min, list_min = heapq.heapqpop(que)
cur.next = ListNode(val_min)
cur = cur.next
lists[list_min] = lists[list_min].next
heapq.heapqpush(que,(lists[list_min],list_min))
return Head.next
问题:
堆排序是从小到大在排序。如果需要从大到小,简单的办法是数字全部变负数,输出结果的时候再改回来
正确代码:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeKLists(self, lists: List[ListNode]) -> ListNode:
Head = ListNode(0)
que = []
cur = Head
for i in range(len(lists)):
if lists[i]: heapq.heappush(que,(lists[i].val,i))
while que:
val_min, list_min = heapq.heappop(que)
cur.next = ListNode(val_min)
cur = cur.next
lists[list_min] = lists[list_min].next
if lists[list_min]: heapq.heappush(que,(lists[list_min].val,list_min))
return Head.next
42.接雨水
class Solution:
def trap(self, height: List[int]) -> int:
end = len(height)
water = 0
for i in range(end):
if i == 0:
continue
if i == end-1:
continue
max1 = max(height[:i])
max2 = max(height[i:])
h = min(max1,max2)-height[i]
if h > 0:
print(i,h)
water += h
return water