【问题标题】:Why is my method creating an extra list within a list? Leetcode 3Sum question为什么我的方法会在列表中创建一个额外的列表? Leetcode 3Sum 问题
【发布时间】:2021-10-19 22:06:05
【问题描述】:

这是leetcode 3sum问题的解决方案:https://leetcode.com/problems/3sum/

我想我有一个解决方案,可能会很慢,但仍然是一个解决方案。但我似乎无法弄清楚为什么我不断收到额外的清单。基本上为input:nums = [-1,0,1,2,-1,-4]
我应该从我的代码中返回输出:[[-1,-1,2],[-1,0,1]].
但我得到的是:[[-1, 2, 1], [-1, 1, 0], [-1, 1, 0]]

我尝试使用它来删除重复的列表,但我认为有一些逻辑错误导致了这个额外的列表。这是删除列表中重复列表的代码,没有提供任何有用的输出,只是内存位置。

代码:

dup_set = set(map(tuple,a))
non_dup_set = map(list,dup_set)

我还想我可能需要一个 hashmap,以便该方法知道不重用索引,但我对 hashmap 的理解不是很好,我只是认为键值,但除此之外没有。

这是完整的代码:

class Solution:
    def threeSum(self, nums):
        nums.sort()
        l = 0
        r = len(nums) - 1
        final_list = []
        
        while l < r:
            if nums[l] + nums[r] <= 0:
                print(nums[l+1:r])
                if (nums[l] + nums[r]) in nums[l+1:r]:
                    final_list.append([nums[l],nums[r],(nums[l]+nums[r])])
                l+=1
            elif nums[l] + nums[r] >= 0:
                print(nums[l+1:r])
                if -(nums[l] + nums[r]) in nums[l+1:r]:
                    final_list.append([nums[l],nums[r],(nums[l]+nums[r])])
                r-=1

        print(final_list)
            
            
Solution().threeSum([-1,0,1,2,-1,-4])

输出:[[-1, 2, 1], [-1, 1, 0], [-1, 1, 0]]

所需:[[-1, 2, 1], [-1, 1, 0]]

请帮忙 谢谢

【问题讨论】:

  • 你的逻辑有缺陷。无论nums[l] + nums[r] 的符号如何,您都需要该值的负数出现在列表中,而不是正数。您不需要那里的第一个 if 子句。然而,这并不能消除重复。
  • 请注意,例如,[-1,2,1] 是不正确的:和是 2,而不是 0。

标签: python loops if-statement


【解决方案1】:

我认为你可以这样:

non_dup_set = list(set(map(tuple, a)))

【讨论】:

    【解决方案2】:

    考虑这个答案。它得到正确的元组,尽管顺序不同。不知道为什么这很重要。

    class Solution:
        def threeSum(self, nums):
            final_list = []
            for l, v1 in enumerate(nums):
                for r, v2 in enumerate(nums[l+1:]):
                    if -(v1+v2) in nums[l+r+1:]:
                        check = sorted((v1,v2,-(v1+v2)))
                        if check not in final_list:
                            final_list.append(check)
            print(final_list)
    
    Solution().threeSum([-1,0,1,2,-1,-4])
    

    更好的答案是使用itertools.combinations 来简化事情:

    import itertools
    class Solution:
        def threeSum(self, nums):
            final_list = []
            for subset in itertools.combinations(nums,3):
                if sum(subset) == 0:
                    check = sorted(subset)
                    if check not in final_list:
                        final_list.append(check)
            print(final_list)
    
    Solution().threeSum([-1,0,1,2,-1,-4])
    

    以及强制性的单行:

    import itertools
    class Solution:
        def threeSum(self, nums):
            final_list = list(set(tuple(sorted(subset)) for subset in itertools.combinations(nums,3) if sum(subset) == 0))
            print(final_list)
    
    Solution().threeSum([-1,0,1,2,-1,-4])
    

    【讨论】:

    • 谢谢,但在 [[0,0,0]] 的情况下它失败了。
    • 我不明白你的意思。这些都不会从您的输入中产生 [0,0,0],如果您输入 [0,0,0],它们都会产生 [[0,0,0]] 作为输出,这是完全正确的。我的代码没有问题。
    猜你喜欢
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 2022-12-06
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    相关资源
    最近更新 更多