Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

two sum python

def twoNumberSum(array, targetSum):
    # Save the number seen so far
    seen = set()
    # Traverse the array
    for n in array:
      	# Assume n is the first number
        n2 = targetSum - n  # Calcualte which is the other number needed
        seen.add(n)  # Keep track of all the seen numbers
        if n2 != n and n2 in seen:
            return [n, n2]  # Found it
            
	return []
Comment

two sum python

class Solution:
    def twoSum(self, nums: list[int], target: int) -> list[int]:
        required = {}
        for i in range(len(nums)):
            if target - nums[i] in required:
                return [required[target - nums[i]], i]
            else:
                required[nums[i]] = i
Comment

PREVIOUS NEXT
Code Example
Python :: sklearn cross_val_score scoring metric 
Python :: when was python created 
Python :: exeption python syntax 
Python :: pi python 
Python :: numpy initialize 2d array 
Python :: remove first character from string python 
Python :: python convert string datetime into datetime 
Python :: python to create pandas dataframe 
Python :: int to string python 
Python :: pandas df make set index column 
Python :: python sort two key 
Python :: plot sphere in matplotlib 
Python :: reverse an array python 
Python :: check tensor type tensorflow 
Python :: python keyboardinterrupt 
Python :: pandas groupby aggregate 
Python :: unicodedecodeerror file read 
Python :: python dictionary to array 
Python :: python get last element of iterator 
Python :: python delete text in text file 
Python :: merge three dataframes pandas based on column 
Python :: python tkinter getting labels 
Python :: compress tarfile python 
Python :: skip error python 
Python :: random string generate python of 2.7 
Python :: pip tensorflow 
Python :: if elseif in single line python 
Python :: make screen shot of specific part of screen python 
Python :: create a generator from a list 
Python :: how to encode hexadecimal python 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =