Leetcode Weekly Contest 175

1346. Check If N and Its Double Exist

题意:给定一个数组,检查其中是否存在两个数M,N,满足M*2==N。即满足:

  • i != j
  • 0 <= i, j < arr.length
  • arr[i] == 2 * arr[j]

此外:2 <= arr.length <= 500
思路:数据量很小,直接暴力就行。

class Solution(object):
    def checkIfExist(self, arr):
        if not arr: return False
        
        n = len(arr)
        for i in range(n):
            for j in range(i+1, n):
                if arr[i]*2 == arr[j] or arr[i]/2.0 == arr[j]*1.0:
                    return True
        
        return False

1347. Minimum Number of Steps to Make Two Strings Anagram

题意:给定两个等长的字符串s和字符串t,任意改变字符串t中的字符,导致字符串t中的字母和字符串s是一样的。求最少的改变字母个数。

题意是指:把字符串t中的字母任意改变,使得与字符串s中的字母相同,组成的字母相同但是顺序可以不同,因此只需要进行计数操作即可。

class Solution(object):
    def minSteps(self, s, t):
        hashs = [0]*26
        hasht = [0]*26
        
        for ch in s:
            hashs[ord(ch)-ord('a')] += 1
        
        for ch in t:
            hasht[ord(ch)-ord('a')] += 1
            
        res = 0    
        for i in range(26):
            tmp = hasht[i]-hashs[i]
            if tmp > 0:
                res += tmp
        
        return res

1348. Tweet Counts Per Frequency

题意:实现一个类 TweetCounts中的两个方法:

    1. recordTweet(string tweetName, int time):
      Stores the tweetName at the recorded time (in seconds).
    1. getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime)

Returns the total number of occurrences for the given tweetName per minute, hour, or day (depending on freq) starting from the startTime (in seconds) and ending at the endTime (in seconds).

class TweetCounts(object):
    def __init__(self):
        self.tweetdict = collections.defaultdict(list)  # value为一个数组啊

    def recordTweet(self, tweetName, time):
        self.tweetdict[tweetName].append(time)       

    def getTweetCountsPerFrequency(self, freq, tweetName, startTime, endTime): # [startTime, endTime] 闭区间
        if startTime > endTime or tweetName not in self.tweetdict: return []
        
        if freq == 'minute': intervals = 60
        elif freq == 'hour': intervals = 3600
        else:  intervals = 86400  # day
         
        number = math.ceil((endTime - startTime + 1)*1.0 / intervals)  # 统一时间单位为秒,float类型,这里的类型处理出错了!!!
        res = [0] * (int(number))
        for time in self.tweetdict[tweetName]:  # time
            if startTime <= time <= endTime:
                tmp = math.floor((time-startTime)*1.0 / intervals)
                res[int(tmp)] += 1
        
        return res

1349. Maximum Students Taking Exam

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值