2022.09.16-力扣刷题(2000、2001、162)

这篇博客介绍了LeetCode中三种不同类型的算法问题,包括字符串操作、矩形互换计算以及峰值元素查找。针对每种问题,作者提供了多种解题方法,如暴力破解、利用字典优化和二分查找。这些策略展示了如何有效解决复杂编程挑战。
摘要由CSDN通过智能技术生成

LeetCode 2000 

题目描述

代码实现

class Solution:
    def reversePrefix(self, word: str, ch: str) -> str:
        index = word.find(ch)

        if index == -1:
            return word
        
        else:
          reverse = word[:index+1]
          return reverse[::-1] + word[index+1:]

LeetCode 2001

 2001. 可互换矩形的组数 - 力扣(LeetCode) (leetcode-cn.com)

题目描述 

 代码实现

方法一:暴力破解(超时)

class Solution:
    def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
        """方法一:暴力解题"""
        n = len(rectangles)
        ans = 0

        for i in range(n-1):
            for j in range(i+1, n):
                w1,h1,w2,h2 = rectangles[i][0], rectangles[i][1],rectangles[j][0],rectangles[j][1]

                if w1/h1 == w2/h2:
                    ans += 1
        return ans



class Solution:
    def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
        n = len(rectangles)
        ans = 0

        for i in range(n-1):
            for j in range(i+1, n):
                w1,h1,w2,h2 = rectangles[i][0], rectangles[i][1],rectangles[j][0],rectangles[j][1]

                if w1*h2 == w2*h1:
                    ans += 1
        return ans

 方法二:defaultdict()字典

class Solution:
    def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
        """方法二:defaultdict()"""
        from collections import defaultdict
        dic = defaultdict(int)
        n = len(rectangles)
        ans = 0
        for i in range(n):
            w1,h1= rectangles[i][0], rectangles[i][1]
            
            # 小trick:累加+1+2+3+4...
            ans += dic[h1/w1]
            dic[h1/w1] += 1
        return ans

方法三:defaultdict()字典 + 最大公约数

class Solution:
    def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
        """方法三:defaultdict() + 最大公约数"""
        def gcd(a, b):
            '''辗转相除法'''
            if a < b:
                b,a = a,b
            r = a % b
            while r!=0:
                a = b
                b = r
                r = a % b
            return b

        dic = defaultdict(int)
        n = len(rectangles)
        ans = 0
        for i in range(n):
            w1,h1= rectangles[i][0], rectangles[i][1]

            big = gcd(w1,h1)

            # 小trick:累加+1+2+3+4...
            ans += dic[(w1//big,h1//big)]
            dic[(w1//big,h1//big)] += 1
        return ans

LeetCode 162

162. 寻找峰值 - 力扣(LeetCode) (leetcode-cn.com)

 题目描述

 代码实现

方法一:暴力破解

class Solution:
    def findPeakElement(self, nums: List[int]) -> int:
        # nums[-1] = nums[n] = -∞ 一定存在峰值,因为两端较小
        if len(nums) == 1: return 0

        n = len(nums)
        for i in range(n-1):
            if nums[i] > nums[i+1]:
                return i
        return n-1

方法二:爬坡法(官方解释)

class Solution:
    def findPeakElement(self, nums: List[int]) -> int:
       
        '''方法二:爬坡'''
        def check(i):
            if i==-1 or i==n:
                return float('-inf')
            return nums[i]

        n = len(nums)
        rand = random.randint(0, n-1)
        while not (check(rand-1) < check(rand) > check(rand+1)):
            if check(rand) < check(rand+1):
                rand += 1
            else:
                rand -= 1
        return rand

方法三:二分查找

class Solution:
    def findPeakElement(self, nums: List[int]) -> int:

        '''方法三:二分查找'''
        left=0
        right=len(nums)-1
        while left < right: # 不能等于
            mid = left + (right-left)//2
            if nums[mid] < nums[mid+1]:
                left = mid+1
            else:
                right = mid
        return left

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值