Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的Leetcode问题Python代码有什么问题_Python - Fatal编程技术网

我的Leetcode问题Python代码有什么问题

我的Leetcode问题Python代码有什么问题,python,Python,我在Leetcode中解决了一个问题。它通过了16项测试中的14项。但后来发生了一个错误。我找不到哪里出了错。谢谢大家! 问题:给您一个整数数组nums,您必须返回一个新的计数数组。counts数组具有这样的属性,其中counts[i]是nums[i]右侧较小元素的数量 例如: Input: [5,2,6,1] Output: [2,1,1,0] 我的答覆是: class Solution: def countSmaller(self, nums): new_lis

我在Leetcode中解决了一个问题。它通过了16项测试中的14项。但后来发生了一个错误。我找不到哪里出了错。谢谢大家!

问题:给您一个整数数组nums,您必须返回一个新的计数数组。counts数组具有这样的属性,其中counts[i]是nums[i]右侧较小元素的数量

例如:

Input: [5,2,6,1]
Output: [2,1,1,0] 
我的答覆是:

class Solution:
    def countSmaller(self, nums):

        new_list=[]

        for i in nums:
            count=0
            a=nums.index(i)
            my_list1=nums[a+1:]

            for x in my_list1:
                if x<i:
                    count+=1
            new_list.append(count)
        return new_list   
类解决方案:
def计数更小(自身,nums):
新列表=[]
对于nums中的i:
计数=0
a=单位指数(i)
我的清单1=nums[a+1:]
对于my_列表1中的x:

如果x这应该能为您解决问题,一行理解:

[sum(1 for y in nums[i:] if y < x) for i, x in enumerate(nums)]
#[2, 1, 1, 0]
[求和(1表示nums中的y[i:]如果y
错误是。?失败的测试用例是什么?如果存在重复项怎么办?问题可能是nums.index返回第一个匹配项,因此如果传递类似[5,0,5,0]的数组,代码将在[2,0,1,0]正确时返回[2,0,2,0]