Created
April 17, 2017 22:07
-
-
Save RichardCSantana-zz/5465317cd2ae070d7a2c16def583e721 to your computer and use it in GitHub Desktop.
https://leetcode.com/problems/two-sum java solution
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; | |
import java.util.Map; | |
/** | |
* @author richard.santana | |
*/ | |
public class SumTwo { | |
public static void main(String[] args) { | |
Integer[] input = new Integer[]{11,2,7,15}; | |
Integer target = 9; | |
System.out.println(Arrays.toString(verify(input, target))); | |
} | |
private static Integer[] verify(final Integer[] input,final Integer target) { | |
Map<Integer, Integer> map = new HashMap<>(); | |
for(int i =0; i < input.length;i++){ | |
Integer value = map.get(input[i]); | |
if(value != null){ | |
return new Integer[] {value,i}; | |
}else{ | |
Integer missing = target - input[i]; | |
map.put(missing,i); | |
} | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment