You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LeetCode 2653题本地IDE正常但平台提交报错求助

问题描述

对应LeetCode题目:2653. 滑动子数组的美丽值
给定整数数组nums,找出每个长度为k的子数组的美丽值。子数组的美丽值定义为:若子数组中负整数数量不少于x,则为第x小的负整数;否则为0。返回所有子数组的美丽值数组。

代码实现(原错误版本)
class Solution(object):
    def getSubarrayBeauty(self, nums, k, x):
        """
        :type nums: List[int]
        :type k: int
        :type x: int
        :rtype: List[int]
        """
        eleToFreq = {m: 0 for m in range(-50, 0)}
        i = j = 0
        toReturn = list()
        while j < len(nums):
            if j >= k - 1:
                if nums[j] < 0:
                    eleToFreq[nums[j]] += 1
                a = 0
                for key, value in eleToFreq.items():
                    if value > 0 and a == x - 1:
                        toReturn.append(key)
                        a += 1
                    elif value > 0:
                        a += value
                if a < x :
                    toReturn.append(0)
                if nums[i] < 0:
                    eleToFreq[nums[i]] -= 1
                i += 1
                j += 1
            else:
                if nums[j] < 0:
                    eleToFreq[nums[j]] += 1
                j += 1
        return toReturn


solution = Solution()
v = solution.getSubarrayBeauty([1, -1, -3, -2, 3], 3, 2)  # 预期输出 [-1,-2,-2]
print(v)

v = solution.getSubarrayBeauty([-1, -2, -3, -4, -5], 2, 2)  # 预期输出 [-1,-2,-3,-4]
print(v)

v = solution.getSubarrayBeauty([-3, 1, 2, -3, 0, -3], 2, 1)  # 预期输出 [-3,0,-3,-3,-3]
print(v)

v = solution.getSubarrayBeauty([1,-1,-3,-2,3], 3, 2)  # 预期输出 [-1,-2,-2]
print(v)

v = solution.getSubarrayBeauty([-43], 1, 1)  # 预期输出 [-43]
print(v)
遇到的问题

上述代码在本地IDE(Intellij)运行所有测试用例均符合预期,但提交到LeetCode平台时,以下测试用例出现错误:

  • 测试用例1:nums=[1,-1,-3,-2,3], k=3, x=2,预期输出[-1,-2,-2],实际输出[-3,-3,-2]
  • 测试用例2:nums=[-1,-2,-3,-4,-5], k=2, x=2,预期输出[-1,-2,-3,-4],实际输出[-2,-2,-3,-4]
问题原因与修正方案

问题根源

  1. 字典遍历顺序不稳定:原代码依赖Python字典的插入顺序遍历负整数,但Python 3.7之前的版本中字典是无序的,LeetCode运行环境可能使用了该版本,导致遍历顺序随机,错误选取了更小的负整数。
  2. 美丽值判断逻辑错误:原代码仅在累加值恰好等于x-1时选取当前数,无法处理频率大于1或累加和超过x的场景,逻辑不严谨。

修正后的代码

class Solution(object):
    def getSubarrayBeauty(self, nums, k, x):
        """
        :type nums: List[int]
        :type k: int
        :type x: int
        :rtype: List[int]
        """
        # 初始化所有负整数(-50到-1)的频率为0
        eleToFreq = {m: 0 for m in range(-50, 0)}
        left = 0
        result = []
        
        # 先填充前k-1个元素的频率
        for right in range(k-1):
            num = nums[right]
            if num < 0:
                eleToFreq[num] += 1
        
        # 滑动窗口处理剩余元素
        for right in range(k-1, len(nums)):
            # 将当前右端元素加入频率统计
            curr_num = nums[right]
            if curr_num < 0:
                eleToFreq[curr_num] += 1
            
            # 计算当前窗口的美丽值:从小到大遍历负整数,累加频率找第x小的
            count = 0
            beauty = 0
            for num in range(-50, 0):
                count += eleToFreq[num]
                if count >= x:
                    beauty = num
                    break
            result.append(beauty if count >= x else 0)
            
            # 移除窗口左端元素的频率
            left_num = nums[left]
            if left_num < 0:
                eleToFreq[left_num] -= 1
            left += 1
        
        return result

修正说明

  1. 固定遍历顺序:不再依赖字典的遍历顺序,而是手动从-50到-1依次遍历负整数,确保按从小到大的顺序统计频率,保证结果的正确性。
  2. 优化美丽值判断逻辑:通过累加频率,当累加和达到或超过x时,当前的负整数就是第x小的,逻辑更严谨,能覆盖所有场景。

内容的提问来源于stack exchange,提问作者Karina

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 21:05:06