力扣每日一题

2023.10.9

#思路:分为两数,位数之差不大于1,然后依次将排序的数放到两个数中
class Solution(object):
    def splitNum(self, num):
        """
        :type num: int
        :rtype: int
        """
        sortedNum  = "".join(sorted(str(num)))
        num1, num2 = int(sortedNum[::2]), int(sortedNum[1::2])
        return num1 + num2

2023.10.10 

#思路:不需要管回头,权当穿过彼此,计算d秒后所在位置
#     然后从小到大排序,计算距离差的和(且不重复)
#     这里用到的方法是两两之间的重复次数,再sum即可
class Solution(object):
    def sumDistance(self, nums, s, d):
        """
        :type nums: List[int]
        :type s: str
        :type d: int
        :rtype: int
        """
        mod = 10**9 + 7
        n = len(nums)
        res = [nums[i]-d if s[i]=='L' else nums[i]+d for i in range(n)]
        res.sort()
        ans = 0
        for i in range(1,n):
            ans += (res[i]-res[i-1]) * i * (n-i) 
        return ans % mod

2023.10.11

class Solution(object):
    def topStudents(self, positive_feedback, negative_feedback, report, student_id, k):
        """
        :type positive_feedback: List[str]
        :type negative_feedback: List[str]
        :type report: List[str]
        :type student_id: List[int]
        :type k: int
        :rtype: List[int]
        """
        #TLE  采取哈希表+排序
        words = {}
        for w in positive_feedback:
            words[w] = 3
        for w in negative_feedback:
            words[w] = -1

        A = []
        for s, i in zip(report, student_id):
            score = sum(words.get(w,0) for w in s.split())
            A.append([-score,i])
        
        A.sort()

        return [resID for _,resID in A[:k]]

#暴力会超时
#student_score = [0] * len(report)
#tmpStr = [0] * len(report)
#
#for i in range(len(report)):
#    tmpStr.extend(report[i].split())
#    for j in range(len(tmpStr)):
#        if tmpStr[j] in positive_feedback:
#            student_score[i] += 3
#        elif tmpStr[j] in negative_feedback:
#            student_score[i] -= 1
#        else:
#            continue
#    tmpStr = []
#
#idx_sorted = [x for _,x in sorted(zip(student_score,student_id), key=lambda pair: (-#pair[0], pair[1]))]
#
##print(student_score)
#return idx_sorted[:k]

2023.10.12 

#写的像个语法题
class Solution(object):
    def findTheArrayConcVal(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        num_str = ''
        ans = 0
        while len(nums) > 1:
            num_str = str(nums[0]) + str(nums[-1])
            ans += int(num_str)
            nums = nums[1:-1]
        if len(nums) == 1:
            ans += nums[0]
        
        return ans

2023.10.13

#贪心 + 二分查找
from sortedcontainers import SortedList
class Solution(object):
    def avoidFlood(self, rains):
        """
        :type rains: List[int]
        :rtype: List[int]
        """
        ans = [1] * len(rains)
        st = SortedList()
        mp = {}
        for i, rain in enumerate(rains):
            if rain == 0:
                st.add(i)
            else:
                ans[i] = -1
                if rain in mp:
                    it = st.bisect(mp[rain])
                    if it == len(st):
                        return []
                    ans[st[it]] = rain
                    st.discard(st[it])
                mp[rain] = i
        return ans

2023.10.14

class Solution(object):
    def singleNumber(self, nums):
        #array = set()
        #res = [0] * 4 * 10**4
        #for i in range(len(nums)):
            #array.add(nums[i])
            #res[nums[i]]+=1
        #for i in range(len(nums)):
            #if res[nums[i]] == 1:
                #return nums[i]
        #异或(自身异或为0,0与自身异或为自身)
        return reduce(lambda x, y: x ^ y, nums)

 2023.10.15

class Solution(object):
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        #哈希映射
        freq = collections.Counter(nums)
        ans = [num for num, occ in freq.items() if occ == 1][0]
        return ans

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值