此代码有效吗? #leetcode #two_sum

发布于 2025-02-06 00:48:50 字数 695 浏览 1 评论 0原文

这是两个总和 leetcode的问题,我试图解决,它被接受了。我询问此代码在内存和空间复杂性方面是否足够有效。

我的代码:

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        
        l = len(nums)
        ans = []
        for i in range(l):
            compliment = target - nums[i];
            # print(a.index(x))
            if compliment in nums:
                if nums.index(compliment)!=i:
                    # print(a.index(x))
                    ans.append(i)
                    ans.append(nums.index(compliment))
                    break;
        return ans

This is two sum problem from leetcode, I tried to solve, It got accepted. I am asking if this code is efficient enough in terms of memory and space complexity.

My code :

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        
        l = len(nums)
        ans = []
        for i in range(l):
            compliment = target - nums[i];
            # print(a.index(x))
            if compliment in nums:
                if nums.index(compliment)!=i:
                    # print(a.index(x))
                    ans.append(i)
                    ans.append(nums.index(compliment))
                    break;
        return ans

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ら栖息 2025-02-13 00:48:51

您的代码在nums中搜索Affiment,然后以索引函数对其进行跟进。稍后,您在nums.index(Affiment)中再次使用此索引。本质上,您正在搜索三次的数组。一个更好的方法是搜索数组并存储索引,如果找到了-1。如果索引不是-1,即,在数组中存在,则可以将其附加到ANS。这本质上跳过了两行(以及索引函数),然后一次穿越数组而不是三次。

index_if_found = -1
for index in range(len(arr)):
    if arr[index] == search_value:
        index_if_found = index
        break

现在,您可以使用此索引index_if_found,而不是使用index函数。

编辑:感谢Kelly在搜索算法上纠正我。


此外,您有两个附加在同一数组中的方法。一种稍快的方法是使用扩展方法并将它们添加到同一操作中。所以

ans.append(i)
ans.append(nums.index(compliment))

你没有你

ans.extend((i, nums.index(compliment)))

Your code searches for compliment in nums, and then follows it up with an index function. Later on, you use this index again in nums.index(compliment). Essentially, you're searching through the array three times. A better way would be to search through the array and store the index if found, else -1. If the index is not -1, i.e., present in the array, you can append it to ans. This essentially skips the two lines (and that index function) and traverses the array once instead of thrice.

index_if_found = -1
for index in range(len(arr)):
    if arr[index] == search_value:
        index_if_found = index
        break

You can now use this index index_if_found instead of using the index function.

EDIT: Thanks to Kelly for correcting me on the search algorithm.


Additionally, you have two append methods to the same array. A slightly faster approach would be to use the extend method and add them in the same operation. So instead of

ans.append(i)
ans.append(nums.index(compliment))

You'd have

ans.extend((i, nums.index(compliment)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文