Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/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
Python二和蛮力方法_Python_Arrays_Loops - Fatal编程技术网

Python二和蛮力方法

Python二和蛮力方法,python,arrays,loops,Python,Arrays,Loops,我是Python新手,刚刚开始尝试使用LeetCode来构建我的排骨。在这个经典问题上,我的代码遗漏了一个测试用例 问题如下: 给定一个整数数组,返回两个数字的索引,使它们相加到一个特定的目标 您可以假设每个输入都有一个解决方案,并且不能两次使用同一个元素 例如: 给定nums=[2,7,11,15],target=9 因为nums[0]+nums[1]=2+7=9, 返回[0,1] 我错过了目标数为6的测试用例[3,2,4],它应该返回[1,2]的索引,但击中了目标数为6的测试用例[1,5,7

我是Python新手,刚刚开始尝试使用LeetCode来构建我的排骨。在这个经典问题上,我的代码遗漏了一个测试用例

问题如下:

给定一个整数数组,返回两个数字的索引,使它们相加到一个特定的目标

您可以假设每个输入都有一个解决方案,并且不能两次使用同一个元素

例如:

给定nums=[2,7,11,15],target=9

因为nums[0]+nums[1]=2+7=9, 返回[0,1]

我错过了目标数为6的测试用例[3,2,4],它应该返回[1,2]的索引,但击中了目标数为6的测试用例[1,5,7](它当然返回索引[0,1]),因此我的while循环中似乎出了问题,但我不太确定是什么

class Solution:
    def twoSum(self, nums, target):
        x = 0
        y = len(nums) - 1
        while x < y:
            if nums[x] + nums[y] == target:
                return (x, y)
            if nums[x] + nums[y] < target:
                x += 1
            else:
                y -= 1
        self.x = x
        self.y = y
        self.array = array       
        return None

test_case = Solution()    
array = [1, 5, 7]
print(test_case.twoSum(array, 6))
类解决方案:
def twoSum(自身、nums、目标):
x=0
y=len(nums)-1
当x

在目标为6的测试用例[3,2,4]上,输出返回null,因此索引1和2甚至没有被汇总,我是否会将y赋值错误

有点不同的方法。我们将根据需要构建一个值字典,该字典由我们要查找的值键入。如果我们查找一个值,我们将在该值首次出现时跟踪该值的索引。一旦您找到满足问题的值,您就完成了。这个时间也是O(N)

输出:

(0, 1)
(1, 2)
(0, 1)
我试着用你的例子来学习。我对我的发现感到高兴。我使用
itertools
为我制作了所有数字的组合。然后我使用列表操作对输入数组中所有可能的数字组合求和,然后我只检查一次目标是否在和数组中。如果不是,则返回None,否则返回索引。请注意,如果这三个索引的总和达到目标值,那么这种方法也将返回所有这三个索引。抱歉花了这么长时间:)

输出

[2, 4]

强力解决方案是在列表上双嵌套一个循环,其中内部循环只查看大于外部循环当前所在索引的索引

class Solution:
    def twoSum(self, nums, target):
        for i, a in enumerate(nums, start=0):
            for j, b in enumerate(nums[i+1:], start=0):
                if a+b==target:
                    return [i, j+i+1]

test_case = Solution()
array = [3, 2, 4]
print(test_case.twoSum(array, 6))

array = [1, 5, 7]
print(test_case.twoSum(array, 6))

array = [2, 7, 11, 15]
print(test_case.twoSum(array, 9))
输出:

[1, 2]
[0, 1]
[0, 1]

这是一个更全面、更高效的方法,即使代码行更短

nums = [6, 7, 11, 15, 3, 6, 5, 3,99,5,4,7,2]
target = 27
n = 0

for i in range(len(nums)):
    
    n+=1
    if n == len(nums):
      n == len(nums)
      
    else:
        if nums[i]+nums[n] == target:
          # to find the target position 
          print([nums.index(nums[i]),nums.index(nums[n])])
          # to get the actual numbers to add print([nums[i],nums[n]])

您的解决方案仅适用于排序列表。此外,请确保x和y彼此不相等。我明白了,我觉得我应该从使用enumerate开始这样做。引导更简单、更高效。这始终是一个学习过程,但我们不想在不需要的时候重新发明轮子:P。很高兴能提供帮助。
class Solution:
    def twoSum(self, nums, target):
        for i, a in enumerate(nums, start=0):
            for j, b in enumerate(nums[i+1:], start=0):
                if a+b==target:
                    return [i, j+i+1]

test_case = Solution()
array = [3, 2, 4]
print(test_case.twoSum(array, 6))

array = [1, 5, 7]
print(test_case.twoSum(array, 6))

array = [2, 7, 11, 15]
print(test_case.twoSum(array, 9))
[1, 2]
[0, 1]
[0, 1]
nums = [6, 7, 11, 15, 3, 6, 5, 3,99,5,4,7,2]
target = 27
n = 0

for i in range(len(nums)):
    
    n+=1
    if n == len(nums):
      n == len(nums)
      
    else:
        if nums[i]+nums[n] == target:
          # to find the target position 
          print([nums.index(nums[i]),nums.index(nums[n])])
          # to get the actual numbers to add print([nums[i],nums[n]])