Skip to content

Commit 34c76bb

Browse files
Added Two Sum problem solution in Java
1 parent c62a3ba commit 34c76bb

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int[] twoSum(int[] nums, int target) {
5+
Map<Integer, Integer> map = new HashMap<>();
6+
for (int i = 0; i < nums.length; i++) {
7+
int complement = target - nums[i];
8+
if (map.containsKey(complement)) {
9+
return new int[]{map.get(complement), i};
10+
}
11+
map.put(nums[i], i);
12+
}
13+
return new int[]{};
14+
}
15+
}

index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Welcome to **Data Structures and Algorithms**, your friendly initiation into the
1010
- [Backspace_String_Compare](Solved-Problems/Backspace_String_Compare "goto Backspace_String_Compare")
1111
- [Longest_Substring_Without_Repeating_Characters](Solved-Problems/Longest_Substring_Without_Repeating_Characters "goto Longest_Substring_Without_Repeating_Characters")
1212
- [Valid_Anagram](Solved-Problems/Valid_Anagram "goto Valid_Anagram")
13+
- [Two Sum](./Two_Sum/Solution.java "goto Two_Sum problem and finding target in an array LeetCode - 1")
1314
<!-- TABLE OF CONTENT ENDS -->
1415

1516
# Table of Contribution

0 commit comments

Comments
 (0)