反转链表 递归

剑指 Offer 24. 反转链表

https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/
定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        if head==None or head.next==None:  #停止迭代的条件
            return head 
        newhead=self.reverseList(head.next)   #F(head.next)
        head.next.next=head   #反转head和head.next
        head.next=None         #反转head和head.next
        return newhead

分析过程:
问题是解决F(head) 即反转以head开头的链表
递推公式 F(head)=F(head.next)+反转head和head.next
停止递推的条件head.next==none or head==None(防止空链表
这一分析方法参考:https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/solution/fan-zhuan-lian-biao-by-leetcode-solution-jvs5/790539

路径总和

给你二叉树的根节点 root 和一个表示目标和的整数 targetSum ,判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。
https://leetcode-cn.com/problems/path-sum/

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
        if root==None:
            return False
        if root.left==None and root.right==None and targetSum!=root.val:
            return False
        if root.left==None and root.right==None and targetSum==root.val:
            return True
        # res1,res2=False,False
        # if root.left!=None:
        #     res1 = self.hasPathSum(root.left,targetSum-root.val)
        # if root.right!=None:
        #     res2 = self.hasPathSum(root.right,targetSum-root.val)
        # return res1 or res2
        return self.hasPathSum(root.left,targetSum-root.val) or self.hasPathSum(root.right,targetSum-root.val)

分析过程:
F(root,target)表示从root到叶子节点是否存在路径和为target;
递推公式:F(root,target)=F(root.left,target-root.val) or F(root.right,target-root.val)
停止递推的条件:root为None 或 root为叶子节点

多数元素

给定一个大小为 n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。

你可以假设数组是非空的,并且给定的数组总是存在多数元素。

class Solution:
    def majorityElement(self, nums: List[int]) -> int:
        #分治法
        def fenzhi(l,r):
            if l==r:                #不断二分,直到只剩一个数时返回该数
                return nums[l]
            mid = (l+r)//2          #二分的中点
            left=fenzhi(l,mid)      #返回左侧众数
            right=fenzhi(mid+1,r)   #右侧众数
            if left == right:       #左右侧众数相等则返回该众数
                return left
            else:                   #不等,则在整个区间内分别统计左右众数出现次数,返回次数大的
                lcount=0
                rcount=0
                for i in nums[l:r+1]:
                    if i==left:
                        lcount+=1
                    if i==right:
                        rcount+=1
                return left if lcount>rcount else right
        return fenzhi(0,len(nums)-1)
        #投票法
        # count=0
        # for num in nums:
        #     if count==0:
        #         res=num
        #         count+=1
        #     elif res!=num:
        #         count-=1
        #     else:
        #         count+=1
        # return res

分析: 分治法
如果数 a 是数组 nums 的众数,如果我们将 nums 分成两部分,那么 a 必定是至少一部分的众数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值