Skip to content

Commit 9a9d259

Browse files
committed
Added : Two Sum
1 parent a078848 commit 9a9d259

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
2+
3+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
4+
5+
Example:
6+
7+
Given nums = [2, 7, 11, 15], target = 9,
8+
9+
Because nums[0] + nums[1] = 2 + 7 = 9,
10+
return [0, 1].
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
@lc id : 1
3+
@problem : Two Sum
4+
@author : rohit
5+
@url : https://leetcode.com/problems/two-sum/
6+
*/
7+
8+
class Solution {
9+
public int[] twoSum(int[] nums, int target) {
10+
11+
HashMap<Integer, Integer> map = new HashMap<>();
12+
13+
for(int i = 0; i < nums.length; i++){
14+
15+
if( map.containsKey( target - nums[i] )
16+
&& map.get(target - nums[i]) != i)
17+
return new int[]{i, map.get(target - nums[i])};
18+
map.put(nums[i], i);
19+
}
20+
21+
return null;
22+
}
23+
}

LeetCode/Array/Template/Problem Statement.txt

Whitespace-only changes.

LeetCode/Array/Template/Solution.java

Whitespace-only changes.

0 commit comments

Comments
 (0)