53. 最大子数组和
复杂度为o(n)的解法:
遍历数组,设置局部最大和和全局最大和,当前局部最大和=max(当前遍历的数,当前遍历的数+局部最大和),因为如果之前的和加上正数会更大,但是如果这个正数本身值就很大,比之前加起来的还大,那么可以从这个数开始计算。更新全局最大和。
class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
max_current=max_global=nums[0]
for i in range(1, len(nums)):
max_current=max(max_current+nums[i],nums[i])
if max_global<max_current:
max_global=max_current
return max_global
分治法的话(复杂度为o(nlogn)),需要将数组分成左边和右边两个部分,但连续数组的最大和的查找需要三个部分,完全在左边,经过中间点,完全在右边。
需要写2个子函数,返回三个部分的最大值和查找经过中间点的最大值。
class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
def max_three_part (nums,left, right):
if left==right:
return nums[left]
mid=(left+right)//2
max_left=max_three_part(nums, left, mid)
max_right=max_three_part(nums, mid+1, right)
max_mid=max_cross_mid(nums, left, right, mid)
return max(max_left,max_right, max_mid)
def max_cross_mid(nums, left, right, mid):
sum_max_left=float('-inf')
sum_max_right=float('-inf')
current_sum=0
for i in range(mid, left-1, -1):
current_sum+=nums[i]
if current_sum>sum_max_left:
sum_max_left=current_sum
current_sum=0
for i in range(mid+1, right+1):
current_sum+=nums[i]
if current_sum>sum_max_right:
sum_max_right=current_sum
return sum_max_left+sum_max_right
return max_three_part(nums,0, len(nums)-1)
56. 合并区间
遍历数组,先将数组按照第一位数进行排序,如果遍历的数的最小值小于前面的最大值就合并这两个数。
class Solution(object):
def merge(self, intervals):
"""
:type intervals: List[List[int]]
:rtype: List[List[int]]
"""
if len(intervals)==1:
return intervals
intervals.sort(key=lambda x: x[0])
merged=[intervals[0]]
for interval in intervals[1:]:
if interval[0]<=merged[-1][1]:
merged[-1][1]=max(merged[-1][1],interval[1])
else:
merged.append(interval)
return merged
94. 二叉树的中序遍历
递归:
class Solution(object):
def inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
ans = []
self._inorderTraversal(root, ans)
return ans
def _inorderTraversal(self, node, ans):
if node is None:
return
self._inorderTraversal(node.left, ans)
ans.append(node.val)
self._inorderTraversal(node.right, ans)
非递归用栈:
借鉴leetcode评论1用颜色白色和灰色标记是否访问节点:
如果节点是白色,说明没有被访问过,标记成灰色,将右节点,节点和左节点入栈
如果节点是灰色,节点输出
class Solution(object):
def inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
white, gray=0, 1
res=[]
stack=[(white,root)]
while stack:
color,node=stack.pop()
if node is None: continue
if color==white:
stack.append((white,node.right))
stack.append((gray,node))
stack.append((white,node.left))
else:
res.append(node.val)
return res
21. 合并两个有序链表
注意空指针
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def mergeTwoLists(self, list1, list2):
"""
:type list1: Optional[ListNode]
:type list2: Optional[ListNode]
:rtype: Optional[ListNode]
"""
l=ListNode()
ans=l
while list1 and list2:
if list1.val<list2.val:
ans.next=list1
list1=list1.next
else:
ans.next=list2
list2=list2.next
ans=ans.next
if list1:
ans.next=list1
else:
ans.next=list2
return l.next