Skip to content

Commit 072a5f8

Browse files
authored
Update 0001_Two_Sum.java
1 parent 8bf3a32 commit 072a5f8

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

Java/0001_Two_Sum.java

+25-25
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
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;
44

55
class Solution {
66
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
88
int diff;
99

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)
1111

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
1313
diff = target - nums[i];
1414

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
1616
result[0] = diff_Map.get(diff); // result[0] = diff's index
1717
result[1] = i; // result[1] = current index
1818

1919
return result; // diff's index and current index are two sum combination
2020
}
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
2222
}
2323
return result;
2424
}
2525

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+
// }
4343
}

0 commit comments

Comments
 (0)