Skip to content

Commit

Permalink
Solution for Two Sum (#8)
Browse files Browse the repository at this point in the history
* Solution for Two Sum

* Address issues in PR#8

---------

Co-authored-by: Trishit Chakraborty <[email protected]>
  • Loading branch information
trishit2801 and Trishit Chakraborty authored Jul 13, 2024
1 parent 65eac77 commit 49ca7dc
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions leetcode/01-two-sum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package leetcode;
import java.util.*;
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] res = new int[2];
HashMap<Integer,Integer> map = new HashMap<>();
for(int i=0; i<nums.length; i++){
if(map.containsKey(target - nums[i])){
res[0] = map.get(target - nums[i]);
res[1] = i;
break;
}
map.put(nums[i], i);
}
return res;
}
}

0 comments on commit 49ca7dc

Please sign in to comment.