[LeetCode]#167. Two Sum II — Input array is sorted

Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
class Solution:
def twoSum(self, nums, target):
hash={}
for i in range(0,len(nums)):
hash[nums[i]]=i
for i in range(0,len(nums)):
if target-nums[i] in hash:
if hash[target-nums[i]] != i:
return [i+1, hash[target-nums[i]]+1]
return []

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fatboy Slim

Fatboy Slim

Interesting in any computer science.