LeetCode 每日一题 2021/12/20-2021/12/26

记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步




12/20 475. 供暖器

将房屋 供暖器从小到大排序
对于每个房屋 找到其距离左右的两个供暖器 其中最短的距离为需要距离
双指针l,r 代表左右供暖器
找到heaters[r]位置大于当前房屋的位置
为了使得r必然存在 在heater末尾加入无穷远的一个取暖器

def findRadius(houses, heaters):
    """
    :type houses: List[int]
    :type heaters: List[int]
    :rtype: int
    """
    heaters.sort()
    houses.sort()
    print(houses)
    print(heaters)
    heaters.append(float('inf'))
    ans = 0
    l,r = 0,1
    for h in houses:
        while heaters[r]<h:
            l+=1
            r+=1
        ans = max(ans,min(abs(h-heaters[l]),abs(heaters[r]-h)))
    return ans

12/21 1154. 一年中的第几天

分别获取年月日 判断是否是润年
将月份之前的天数累加 再加上当前月份天数

def dayOfYear(date):
    """
    :type date: str
    :rtype: int
    """
    monthday = [31,28,31,30,31,30,31,31,30,31,30]
    l = date.split("-")
    year = int(l[0])
    if year%4==0 and (year%100!=0 or year==2000):
            monthday[1]=29
    ans =0
    month = int(l[1])
    if month>1:
        ans += sum(monthday[:month-1])
    day = int(l[2])
    ans +=day
    return ans

12/22 686. 重复叠加字符串匹配

1.判断b中是否有a不存在的字符 如果有则一定不能
将n个a拼成长度大于nb的字符串
从头开始比较是否存在和b一样的子字符串
2.对于a,b 做少需要 [b/a]上取整个a 最多需要[b/a]上取整+1个a
判断新字符串内是否存在b

def repeatedStringMatch1(a, b):
    """
    :type a: str
    :type b: str
    :rtype: int
    """
    m={}
    for c in a:
        m[c]=1
    for c in b:
        if c not in c:
            return -1
    na,nb = len(a),len(b)
    n = nb//na+2
    
    newa = "".join([a]*n)
    for i in range(len(newa)-nb+1):
        if newa[i:i+nb]==b:
            left = (nb-(na-i))
            n = left//na+1
            if left%na>0:
                n+=1
            return n
    return -1

def repeatedStringMatch(a, b):
    """
    :type a: str
    :type b: str
    :rtype: int
    """
    na,nb = len(a),len(b)
    n = (nb+na-1)//na
    newa = a*n
    if newa.find(b)!=-1:
        return n
    newa = a*(n+1)
    if newa.find(b)!=-1:
        return n+1
    return -1

12/23 1044. 最长重复子串

  1. 暴力搜索 记录最长答案
  2. 将字符串哈希
    如果哈希值相同则两个字符串相同
    Rabin-Karp算法计算哈希值
    一个底数容易哈希冲突 使用两个大大减小冲突可能
    对于子字符串长度进行二分
def longestDupSubstring1(s):
    """
    :type s: str
    :rtype: str
    """
    n = len(s)
    ans = ""
    for i in range(n):
        if i+len(ans)>=n:
            break
        while s[i:i+len(ans)+1] in s[i+1:]:
            ans = s[i:i+len(ans)+1]
    return ans

def longestDupSubstring(s):
    """
    :type s: str
    :rtype: str
    """
    prime1 = 31
    prime2 = 97
    MOD = 10**9+7
    n = len(s)
    arr = [ord(x)-ord('a') for x in s]
    def check(length):
        h1,h2 = 0,0
        for i in range(length):
            h1 = (h1*prime1+arr[i])%MOD
            h2 = (h2*prime2+arr[i])%MOD
        p1 = pow(prime1,length)%MOD
        p2 = pow(prime2,length)%MOD
        mem = set()
        mem.add((h1,h2))
        for left in range(1,n-length+1):
            h1 = (h1*prime1-arr[left-1]*p1+arr[left+length-1])%MOD
            h2 = (h2*prime2-arr[left-1]*p2+arr[left+length-1])%MOD
            if (h1,h2) in mem:
                return s[left:left+length]
            mem.add((h1,h2))
        return ""
    l,r = 0,n-1
    ans = ""
    while l<=r:
        mid = l+(r-l+1)//2
        cur = check(mid)
        if cur!="":
            ans = cur
            l = mid +1
        else:
            r = mid -1
    return ans

12/24 1705. 吃苹果的最大数目

最小堆l 存储(腐坏日期,苹果数量)
遍历每一天的苹果
先将已经坏了的苹果扔掉
如果当天有苹果 将(腐坏日期,苹果数量)存入堆中
选择腐坏日期最接近的优先拿出来吃一个
更新堆
遍历完生长苹果的日期后 同样将剩余的依次取出

def eatenApples(apples, days):
    """
    :type apples: List[int]
    :type days: List[int]
    :rtype: int
    """
    import heapq
    l = []
    ans = 0
    heapq.heapify(l)
    for i,apple in enumerate(apples):
        while l and l[0][0]<=i:
            heapq.heappop(l)
        if apple:
            heapq.heappush(l,[i+days[i],apple])
        if l:
            l[0][1]-=1
            if l[0][1]==0:
                heapq.heappop(l)
            ans+=1
    day = len(apples)
    while l:
        while l and l[0][0]<=day:
            heapq.heappop(l)
        if len(l)==0:
            break
        d,n = heapq.heappop(l)
        num = min(d-day,n)
        ans +=num
        day +=num
    return ans

12/25 1609. 奇偶树

BFS 广搜
level记录当前层数
每个数v需要满足与level的奇偶性不同
prev记录当前位置前一个数大小 来判断当前层是否递增递减

class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
def isEvenOddTree(root):
    """
    :type root: TreeNode
    :rtype: bool
    """
    level = 0
    l = [root]
    while l:
        tmp = []
        prev = 0
        for i,node in enumerate(l):
            v = node.val
            if level%2+v%2!=1:
                return False
            if i>0:
                if level%2==0:
                    if v<=prev:
                        return False
                else:
                    if v>=prev:
                        return False
            prev = v
            if node.left:
                tmp.append(node.left)
            if node.right:
                tmp.append(node.right)
        level+=1
        l = tmp[:]
    return True

12/26 1078. Bigram 分词

记录当前单词之前第一个单词 第二个单词的状态 a,b
如果第二单词状态为True 则加入当前单词
如果第一单词状态为True 判断当前单词是否与第二单词相同
判断当前单词与第一单词是否相同 修改第一单词状态

def findOcurrences(text, first, second):
    """
    :type text: str
    :type first: str
    :type second: str
    :rtype: List[str]
    """
    a,b = False,False
    l = text.split(" ")
    ans = []
    for word in l:
        if b:
            b = False
            ans.append(word)
        if a and word==second:
            b = True
        if word == first:
            a = True
        else:
            a = False
    return ans

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值