[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 []

--

--

--

Interesting in any computer science.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

[How-to] create and track Work Orders in AssetSonar

All you need to know about Pointers-Part-2

Best practice for using Jira fields (Part 2/2)

How to use Packer to Build a Debian 10 Template for VMware vSphere

Creating Point & Click Player Movement By Using Raycasting And A Nav Mesh In Unity

The 8 Puzzle Problem

Learn Rust by Developing Application: Apache Parquet Dataset Metadata Viewer

ASP.NET Core MVC with .NET 5.0 ORACLE, Identity using N-tier architecture-Part 01

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.

More from Medium

Leetcode — Asteroid Collision

Sorting Algorithms Explained Using Python: Merge Sort

Glass Cube and Dots

LeetCode #167 | Two Sum II — Input Array Is Sorted (Python)