LeetCode 每日一题 2021/7/5-2021/7/11

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




7/5 726. Number of Atoms 原子的数量

按情况分析
元素s 元素个数num 当前字符c
1.大写字母
判断c之前是否有元素
如果有放入map
重新设置元素s
2.小写字母
之前必定有大写字母 添加入元素s
3.数字
更新num = num*10+int©
4.前括号
如果c之前有元素 将元素放入map
将之前的map放入stack中
5.后括号
如果c之前有元素 将元素放入map
找到’)‘之后的数值tmpv
使用tmpv更新此时tmp中数值
此时stack[-1]必定是一个’(’ 弹出
继续取stack值
如果是’(’ 重新放入stack 继续往后
如果是map 将此map与当前tmp相加
最后结束循环 同样判断是否还要元素s

def countOfAtoms(formula):
    """
    :type formula: str
    :rtype: str
    """
    from collections import defaultdict
    n = len(formula)
    stack = []
    i = 0
    s = ""
    num = 0
    tmp = defaultdict(int)
    while i<n:
        c = formula[i]
        i +=1
        if c>="A" and c<="Z":
            if s:
                if num ==0:
                    num = 1
                tmp[s]+=num
                num = 0
                s = c
            else:
                s = c
                num = 0
        elif c>="a" and c<="z":
            s += c
        elif c>="0" and c<="9":
            num = num*10 + int(c)
        elif c=="(":
            if s:
                if num ==0:
                    num = 1
                tmp[s]+=num
                num = 0
                s = ""
            
            stack.append(tmp)
            tmp = defaultdict(int)
            stack.append("(")
        elif c==")":
            if s:
                if num ==0:
                    num = 1
                tmp[s]+=num
                num = 0
                s = ""
                
            tmpv = 0
            while i<n and  formula[i]>="0" and formula[i]<="9":
                tmpv = tmpv*10+int(formula[i])
                i +=1
                if i == n:
                    break
            if not tmpv:
                tmpv = 1
            for k in tmp.keys():
                tmp[k] *= tmpv
            
            stack.pop(-1)
            while stack:
                sv = stack.pop(-1)
                if sv=="(":
                    stack.append(sv)
                    break
                for k in sv.keys():
                    tmp[k] += sv[k]
    if s:
        if num ==0:
            num = 1
        tmp[s]+=num
               
    key = list(tmp.keys())
    key.sort()
    ans = ""
    for k in key:
        ans += k
        if tmp[k]>1:
            ans += str(tmp[k])
    return ans
    

7/6 1418. Display Table of Food Orders in a Restaurant 点菜展示表

哈希表map
foods,tname set 用来记录所有出现过的桌号和菜名
menu[table] 用来记录table这一桌点的菜
排序 菜名 桌号
依次加入ans中

def displayTable(orders):
    """
    :type orders: List[List[str]]
    :rtype: List[List[str]]
    """
    ans = []
    menu = {}
    foods = set()
    tname = set()
    for order in orders:
        table,food = order[1],order[2]
        tname.add(table)
        foods.add(food)
        m = menu.get(table,{})
        m[food] = m.get(food,0)+1
        menu[table]=m

    firstline = ["Table"]
    foods = list(foods)
    foods.sort()
    firstline.extend(foods)
    ans.append(firstline)
    
    tname = list(tname)
    tname.sort(key= lambda x: int(x))
    for name  in tname:
        order = [name]
        for food in foods:
            if food in menu[name]:
                order.append(str(menu[name][food]))
            else:
                order.append("0")
        ans.append(order)
    return ans
    

7/7 1711. Count Good Meals 大餐计数

1.哈希表存储 比较
超时
2.用哈希表存储位置i之前的所有数个数
遍历2的幂 找到能与i位置满足的数
最大数maxn 那么最大的2的幂<2*maxn

def countPairs(deliciousness):
    """
    :type deliciousness: List[int]
    :rtype: int
    """
    mod = 10**9+7
    from collections import defaultdict
    m = defaultdict(int)
    for i in deliciousness:
        m[i]+=1
    l = list(set(deliciousness))
    
    def check(ans):
        return ans&(ans-1)==0
    
    ans = 0
    for i in range(len(l)-1):
        for j in range(i+1,len(l)):
            if check(l[i]+l[j]):
                ans += m[l[i]]*m[l[j]]
                ans %= mod
    for i in range(len(l)):
        if check(l[i]*2) and m[l[i]]>1:
            ans += m[l[i]]*(m[l[i]]-1)//2
            ans %= mod
    return ans

def countPairs2(deliciousness):
    """
    :type deliciousness: List[int]
    :rtype: int
    """
    mod = 10**9+7
    from collections import defaultdict
    m = defaultdict(int)
    maxn = max(deliciousness)*2
    ans = 0
    for num in deliciousness:
        s=1
        while s<=maxn:
            ans += m[s-num]
            ans %= mod 
            s = s<<1
        m[num]+=1
    return ans
    

7/8 930. Binary Subarrays With Sum 和相同的二元子数组

前缀和 m用来记录前缀和

def numSubarraysWithSum(nums, goal):
    """
    :type nums: List[int]
    :type goal: int
    :rtype: int
    """
    m={}
    m[0]=1
    s = 0
    ans = 0
    for num in nums:
        s += num
        ans += m.get(s-goal,0)
        m[s] = m.get(s,0)+1
    return(ans)
    

7/9 面试题 17.10. 主要元素

1.先排序 如果存在出现次数多于一半的数 必定在中间位置
2.摩尔投票 找到出现次数最多的数 再计算它出现的次数是否多于一半

def majorityElement(nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    n=len(nums)
    nums.sort()
    mid=n//2
    l = mid
    while l>=0:
        if nums[l]==nums[mid]:
            l-=1
        else:
            break
    
    r = mid
    while r<n:
        if nums[r]==nums[mid]:
            r+=1
        else:
            break
        
    num = r-l-1
    return nums[mid] if num>=n//2+1 else -1

def majorityElement2(nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    n=len(nums)
    count = 0
    curr = -1
    for num in nums:
        if count==0:
            curr = num
            count=1
        elif curr == num:
            count+=1
        else:
            count-=1
    s = 0
    print(curr)
    for num in nums:
        if curr==num:
            s+=1
    
    return curr if s>=n//2+1 else -1
    

7/10 981. Time Based Key-Value Store 基于时间的键值存储

valuemap 用来存储key键下的value
stampmap 用来存储key键下的时间戳
因为set时间戳是递增的 所以stampmap[key] = [xx,xx,xx] 中的list是个严格递增的序列
因此可以使用二分查找找到不大于timestamp的最大值

from collections import defaultdict
class TimeMap(object):

    
    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.valuemap = defaultdict(list)
        self.stampmap = defaultdict(list)


    def set(self, key, value, timestamp):
        """
        :type key: str
        :type value: str
        :type timestamp: int
        :rtype: None
        """
        self.valuemap[key].append(value)
        self.stampmap[key].append(timestamp)


    def get(self, key, timestamp):
        """
        :type key: str
        :type timestamp: int
        :rtype: str
        """
        stamplist = self.stampmap[key]
        if not stamplist:
            return ""
        if timestamp<stamplist[0]:
            return ""
        if timestamp>=stamplist[-1]:
            return self.valuemap[key][-1]
        
        l,r = 0,len(stamplist)-1
        print(stamplist)
        while l<=r:
            mid = (l+r)>>1
            print(l,r,mid)
            if stamplist[mid]==timestamp:
                return self.valuemap[key][mid]
            if stamplist[mid]<timestamp:
                l = mid+1
            else:
                r = mid-1
        if r>=0:
            return self.valuemap[key][r]
        return ""
    

7/11 274. H-Index H 指数

两层二分
最大的可能数 = min(len(citations),max(citations))
check:
二分判断num是否满足h指数条件
二分寻找最大的可能性num

def hIndex(citations):
    """
    :type citations: List[int]
    :rtype: int
    """
    if not citations:
        return 0
    citations.sort()
    n = min(len(citations),max(citations))
    def check(num):
        l,r = 0,len(citations)-1
        while l<=r:
            mid = (l+r)>>1
            if citations[mid]>=num:
                r = mid-1
            else:
                l = mid+1
        if len(citations)-r-1>=num:
            return True
        return False
    
    l,r = 0,n
    ans = 0
    while l<=r:
        mid = (l+r)>>1
        if check(mid):
            ans = mid
            l = mid+1
        else:
            r = mid-1
    return ans
    

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值