-
-
Save techisbeautiful/f25b0451d30fd7258cb964af05be4eb9 to your computer and use it in GitHub Desktop.
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
import java.util.Arrays; | |
import java.util.HashMap; | |
class TwoSumSolution2{ | |
static int[] findTwoSumSolution2(int[] nums, int target) { | |
HashMap<Integer,Integer> map = new HashMap<>(); | |
for(int i = 0; i < nums.length; i++){ | |
Integer temp = (target - nums[i]); | |
if(map.containsKey(temp)){ | |
int result [] = { map.get(temp), i }; | |
return result; | |
} | |
map.put(nums[i], i); | |
} | |
return null; | |
} | |
public static void main(String [] args) { | |
int testCase1nums[] = {5, 7, 12,1}; | |
int testCase1Target = 12; | |
int testCase2nums[] = {3, 5, 0}; | |
int testCase2Target = 6; | |
int testCase3nums[] = {0, 3}; | |
int testCase3Target = 3; | |
int testCase4nums[] = {0, -1, 2, -3, 1}; | |
int testCase4Target = -2; | |
int testCase5nums[] = {4, -11, 22, -23, 31}; | |
int testCase5Target = -20; | |
System.out.println(Arrays.toString(findTwoSumSolution2(testCase1nums,testCase1Target))); | |
System.out.println(Arrays.toString(findTwoSumSolution2(testCase2nums,testCase2Target))); | |
System.out.println(Arrays.toString(findTwoSumSolution2(testCase3nums,testCase3Target))); | |
System.out.println(Arrays.toString(findTwoSumSolution2(testCase4nums,testCase4Target))); | |
System.out.println(Arrays.toString(findTwoSumSolution2(testCase5nums,testCase5Target))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment