简体   繁体   English

我应该如何修改我的代码以便 twosum 函数工作?

[英]How should I modify my code so that twosum function works?

The question is in Leetcode, which is:问题出在 Leetcode 中,它是:

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.给定一个整数数组 nums 和一个整数目标,返回两个数字的索引,使它们相加为目标。 Each input would have exactly one solution, and you may not use the same element twice.每个输入将只有一个解决方案,并且您不能两次使用相同的元素。

Here is my code for this exercise.这是我用于此练习的代码。

def twoSum(nums: List[int], target: int) -> List[int]:
    for i in range(len(nums)):
        for j in range(1,len(nums)):
            if nums[i]+nums[j]==target:
                return i,j
        return i,j
twoSum([8,3,7,9,2],9)
#Result: (0, 4)

The output is not correct as 8+2=10 ( nums[0] + nums[4] ) not equal to 9.输出不正确,因为8+2=10 ( nums[0] + nums[4] ) 不等于 9。

Why does this happen?为什么会发生这种情况?

You have two issues with your code:您的代码有两个问题:

  • Your second return will just return at the end of the first iteration of the outer loop.您的第二次return将在外循环的第一次迭代结束时返回。 Since i is still at 0 and j is all the way up to len(nums) - 1 , you get (0, 4) as result.由于 i 仍为 0 并且 j 一直到len(nums) - 1 ,因此结果为 (0, 4) 。 Removing it should resolve this issue.删除它应该可以解决这个问题。
  • The innner loop starts with 1 instead of 0, but results like (1, 1) would still be possible.内部循环从 1 而不是 0 开始,但像 (1, 1) 这样的结果仍然是可能的。 It is therefore necessary to adapt the inner loop based on the outer loops current state.因此,有必要根据外环的当前状态调整内环。

A possible solution would look like this:一个可能的解决方案如下所示:

def twoSum(nums: List[int], target: int) -> List[int]:
  for i in range(len(nums)):
    for j in range(i+1, len(nums)):
      if nums[i]+nums[j] == target:
        return i,j
  return -1, -1

The -1, -1 will only occur if there is no solution. -1, -1 只有在没有解决方案时才会出现。 Since it is stated in the question that there is always exactly one solution, this should never happen.由于问题中指出始终只有一个解决方案,因此永远不会发生这种情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python代码中修改文件路径,使其在其他计算机上工作? - How to modify a filepath in Python code so that it works on other computers? Python-为什么我的列表未在此方法范围之外进行修改? 如何修改我的代码? - Python - Why is my list not being modified outside the scope of this method? How do I modify my code so that it is? 我如何修改此代码,以免每次都重新绘制图形,matplotlib - How can I modify this code so that I am not replotting my graph everytime, matplotlib 我应该如何创建我的对象,使其与networkx很好地兼容? - How should I create my object so it works with networkx pretty well? 没有属性“编译”,我如何修改类,使其工作? - No attribute 'compile', how can I modify the class, so that it works? 如何修改我的代码,以便列表中的图像显示在行和图块中? - How can I modify my code so that images from a list appear in rows and tiles? 如何修改我的代码,以便它考虑整数 1 和 0 的特殊操作? - How do I modify my code so that it takes into consideration special operations for integer 1 and 0? 如何修改此代码,使它不返回到函数的开头,而是返回到函数的开头? - How can I modify this code so it doesn't go back to the beginning of the function, but a little bit after the beginning? 如何设置我的路径,以便它与作为 package 本地安装的 Python 代码一起使用 - How can I set my path so that it works with Python code that is locally installed as a package 如何修改我的代码,以便我可以在 Python 中同时搜索多个输入(来自同一列) - How to I modify my code so I can search for multiple inputs(from the same column) at the same time in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM