-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
23 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
interview_prep/algorithm/java/ide_handicapped/two_sum/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Solution { | ||
public static int[] twoSum(int[] nums, int target) { | ||
java.util.Map<Integer, Integer> map = new java.util.HashMap<>(); | ||
for (int i = 0; i< nums.length; i++){ | ||
int com = target - nums[i]; | ||
if(map.containsKey(com)){ | ||
return new int[]{map.get(com),i} ; | ||
} | ||
map.put(nums[i],i); | ||
} | ||
return new int[] {9999,9999}; | ||
} | ||
|
||
public static void main(String[] args) { | ||
//https://leetcode.com/problems/two-sum/description/ | ||
// assert twoSum(new int[]{3,2,4},6 ) == new int[]{1,2} : "{3,2,4} 6 ---> {1,2}"; | ||
// assert twoSum(new int[]{3,3},6 ) == new int[]{0,1} : "{3,3} 6 ---> {0,1}"; | ||
|
||
assert java.util.Arrays.equals(twoSum(new int[]{3,2,4},6 ), new int[]{1,2}); | ||
assert java.util.Arrays.equals(twoSum(new int[]{3,3},6), new int[]{0,1}); | ||
|
||
} | ||
} |