|
1 |
| -//import java.util.Arrays; |
2 |
| -//import java.util.HashMap; |
3 |
| -//import java.util.Map; |
| 1 | +// import java.util.Arrays; |
| 2 | +// import java.util.HashMap; |
| 3 | +// import java.util.Map; |
4 | 4 |
|
5 | 5 | class Solution {
|
6 | 6 | public static int[] twoSum(int[] nums, int target) {
|
7 |
| - int[] result = new int[2]; // instantiate new array |
| 7 | + int[] result = new int[2]; // instantiate new array which will contain twoSum result |
8 | 8 | int diff;
|
9 | 9 |
|
10 |
| - Map<Integer, Integer> diff_Map = new HashMap<Integer, Integer>(); // create hashmap (key: 'nums' element, value: index) |
| 10 | + Map<Integer, Integer> diff_Map = new HashMap<Integer, Integer>(); // create hashmap (key: nums element, value: index) |
11 | 11 |
|
12 |
| - for(int i=0; i<nums.length; i++) { // for all elements in the 'nums' array |
| 12 | + for(int i=0; i<nums.length; i++) { // for all elements in the nums array |
13 | 13 | diff = target - nums[i];
|
14 | 14 |
|
15 |
| - if(diff_Map.containsKey(diff)) { // if the 'diff' exists in the 'map' |
| 15 | + if(diff_Map.containsKey(diff)) { // if the diff exists in the diff_Map |
16 | 16 | result[0] = diff_Map.get(diff); // result[0] = diff's index
|
17 | 17 | result[1] = i; // result[1] = current index
|
18 | 18 |
|
19 | 19 | return result; // diff's index and current index are two sum combination
|
20 | 20 | }
|
21 |
| - diff_Map.put(nums[i], i); // if the 'diff' deosn't exist, add nums[i] as a key and index as a value |
| 21 | + diff_Map.put(nums[i], i); // if the diff deosn't exist, add nums[i] as a key and index as a value |
22 | 22 | }
|
23 | 23 | return result;
|
24 | 24 | }
|
25 | 25 |
|
26 |
| -// public static void main(String[] args) { |
27 |
| -// // Example 1, output: [0, 1] |
28 |
| -// int[] nums1 = {2, 7 ,11, 15}; |
29 |
| -// int[] example1 = twoSum(nums1, 9); |
30 |
| -// |
31 |
| -// // Example 2, output: [1,2] |
32 |
| -// int[] nums2 = {3, 2, 4}; |
33 |
| -// int[] example2 = twoSum(nums2, 6); |
34 |
| -// |
35 |
| -// // Example 3, output: [0,1] |
36 |
| -// int[] nums3 = {3, 3}; |
37 |
| -// int[] example3 = twoSum(nums3, 6); |
38 |
| -// |
39 |
| -// System.out.println(Arrays.toString(example1)); // [0, 1] |
40 |
| -// System.out.println(Arrays.toString(example2)); // [1, 2] |
41 |
| -// System.out.println(Arrays.toString(example3)); // [0, 1] |
42 |
| -// } |
| 26 | + // public static void main(String[] args) { |
| 27 | + // // Example 1, output: [0,1] |
| 28 | + // int[] nums1 = {2, 7 ,11, 15}; |
| 29 | + // int[] example1 = twoSum(nums1, 9); |
| 30 | + |
| 31 | + // // Example 2, output: [1,2] |
| 32 | + // int[] nums2 = {3, 2, 4}; |
| 33 | + // int[] example2 = twoSum(nums2, 6); |
| 34 | + |
| 35 | + // // Example 3, output: [0,1] |
| 36 | + // int[] nums3 = {3, 3}; |
| 37 | + // int[] example3 = twoSum(nums3, 6); |
| 38 | + |
| 39 | + // System.out.println(Arrays.toString(example1)); // [0,1] |
| 40 | + // System.out.println(Arrays.toString(example2)); // [1,2] |
| 41 | + // System.out.println(Arrays.toString(example3)); // [0,1] |
| 42 | + // } |
43 | 43 | }
|
0 commit comments