leetcode 咒语药水+情侣牵手+链表处理(删去操作)详解(干货满满!!!)

如果你看到这里突然一滑动觉得无聊,不妨直接看看下面链表总结,我举例说明了一种很不错的方法,同时还有一些总结心得,干货满满,比我前面这俩个水字数题强太多太多了

2300. 咒语和药水的成功对数
给你两个正整数数组 spells 和 potions ,长度分别为 n 和 m ,
其中 spells[i] 表示第 i 个咒语的能量强度,
potions[j] 表示第 j 瓶药水的能量强度。
同时给你一个整数 success 。一个咒语和药水的能量强度 相乘 
如果 大于等于 success ,那么它们视为一对 成功 的组合。
请你返回一个长度为 n 的整数数组 pairs,
其中 pairs[i] 是能跟第 i 个咒语成功组合的 药水 数目。

题解: 暴力解,分别遍历药水与法术进行匹配 (时间复杂度较大)

双指针解,使用之前总结方法套路,先对其进行排序,便于一步判断,当最大值不满足时直接break,满足时进行移动,(时间复杂度还不行),基于双指针进行优化,采用折半方法,直接一半一半进行判断,同时因为排序操作了,判断更为便捷

class Solution(object):
    def successfulPairs(self, spells, potions, success):
        """
        :type spells: List[int]
        :type potions: List[int]
        :type success: int
        :rtype: List[int]
        """
        count=0
        l=[]
        for x in spells:
            for y in potions:
                if x*y>=success:
                    count+=1
            l.append(count)
            count=0
        return l

class Solution(object):
    def successfulPairs(self, spells, potions, success):
        """
        :type spells: List[int]
        :type potions: List[int]
        :type success: int
        :rtype: List[int]
        """
        potions_l=0
        potions_r=len(potions)-1
        potions.sort()
        res=[]
        count=0
        for x in spells:
            while potions_l<potions_r:
                if x*potions[potions_r]<success:
                    break
                elif x*potions[potions_l]>=success:
                    count+=potions_r-potions_l+1
                    break
                else:
                    potions_l+=1
            res.append(count)
            count=0
            potions_l=0
        return res

class Solution(object):
    def successfulPairs(self, spells, potions, success):
        """
        :type spells: List[int]
        :type potions: List[int]
        :type success: int
        :rtype: List[int]
        """
        l=0
        r=len(potions)-1
        mid=(l+r)/2
        potions.sort()
        count=0
        res=[]
        for x in spells:
            if x*potions[mid]<success:
                l=mid+1
                while l<r:
                    if x * potions[r] < success:
                        break
                    elif x * potions[l] >= success:
                        count += r - l + 1
                        break
                    else:
                        l += 1
            else:
                r=mid-1
                while l<r:
                    if x*potions[l]>=success:
                        count=r-l+1+mid
                    else:
                        l+=1
            res.append(count)
            count=0
            l = 0
            r = len(potions) - 1
            mid = (l + r) / 2
        return res
765. 情侣牵手
n 对情侣坐在连续排列的 2n 个座位上,想要牵到对方的手。
人和座位由一个整数数组 row 表示,其中 row[i] 是坐在第 i 个座位上的人的 ID。
情侣们按顺序编号,
第一对是 (0, 1),第二对是 (2, 3),以此类推,最后一对是 (2n-2, 2n-1)。
返回 最少交换座位的次数,以便每对情侣可以并肩坐在一起。 
每次交换可选择任意两人,让他们站起来交换座位。
示例 1:
输入: row = [0,2,1,3]
输出: 1
解释: 只需要交换row[1]和row[2]的位置即可。
示例 2:
输入: row = [3,2,0,1]
输出: 0
解释: 无需交换座位,所有的情侣都已经可以手牵手了。

题解:遍历每个元素进行判断奇偶数及是否满足,满足则跳过,不满足则计数,并找寻满足位,这一步很重要,每交换一次会影响后面的交换次数,故需要在找到匹配对象后,进行换位

class Solution(object):
    def minSwapsCouples(self, row):
        """
        :type row: List[int]
        :rtype: int
        """
        count = 0
        for i in range(0, len(row), 2):
            x = row[i]
            if row[i+1] != x^1:
                count += 1
                for j in range(i+2, len(row)):
                    if row[j] == x^1:
                        row[i+1], row[j] = row[j], row[i+1]
                        break
        return count
19. 删除链表的倒数第 N 个结点
给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点

题解:    双指针法
        
           1  2  3  4  5  6
        s
              f
        比如说删除倒数第二个元素,则先让f 走俩步
        然后让s f 同时走 当f.next 是空时停止
           1  2  3  4  5  6
                    s
                          f
        此时直接让s.next=s.next.next (跳过,即删去所要求的删去的倒二个元素)

总结:针对链表删减增加都可采用这种方法,建议首先建一个新表用来存储结果

当返回结果时 dummy.next更为方便

要不直接返回原结果,若只有一个元素则会指向null 新建一个表当删到只有一个元素(原表),此时结果next正好指向这个元素,不会为null

 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 removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        # 使用一个新的表 推荐对链表删减增加时使用
        # 这里无所谓,主要作用是减少后续对链表的判断
        # 相当于生成个新链表存储结果,减去对原表的处理,只需要找寻需要的元素就好哦
        dummy=ListNode(0)
        # 相当于 dummy——>1->2...... (举例说明)
        dummy.next=head
        s=f=dummy
        for _ in range(n):
            f=f.next
        while f.next:
            s=s.next
            f=f.next
        s.next=s.next.next
        return dummy.next

  • 24
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

01_

感谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值