22
33class Solution {
44 public int [] twoSum (int [] nums , int target ) {
5- // Approach : using HashMap to get index with the element in O(n) time complexity
5+ // Approach : using HashMap to get index with the element in O(n) time
6+ // complexity
67 // SpaceComplexity is also O(n)
7- HashMap <Integer ,Integer > numIndexMap = new HashMap <Integer ,Integer >();
8+ HashMap <Integer , Integer > numIndexMap = new HashMap <Integer , Integer >();
89
910 // Make key and value HashMap
10- for (int i = 0 ; i < nums .length ; i ++){
11+ for (int i = 0 ; i < nums .length ; i ++) {
1112 int num = nums [i ];
12- numIndexMap .put (num ,i );
13+ numIndexMap .put (num , i );
1314 }
1415
1516 // Search for the other operand looping nums
16- for (int i = 0 ; i < nums .length ; i ++){
17+ for (int i = 0 ; i < nums .length ; i ++) {
1718 int num = nums [i ];
1819 int operand = target - num ;
1920 Integer index = numIndexMap .get (operand );
2021 boolean indexExists = index != null ;
2122 boolean indexExistsAndIndexIsNotTheNum = indexExists && i != index ;
22- if (indexExistsAndIndexIsNotTheNum ){
23+ if (indexExistsAndIndexIsNotTheNum ) {
2324 // Manual sort
24- if ( i < index ){
25- return new int []{ i , index };
26- }else {
27- return new int []{ index , i };
25+ if ( i < index ) {
26+ return new int [] { i , index };
27+ } else {
28+ return new int [] { index , i };
2829 }
2930 }
3031 }
@@ -33,4 +34,4 @@ public int[] twoSum(int[] nums, int target) {
3334 // But the compiler don't know about that
3435 return new int [2 ];
3536 }
36- }
37+ }
0 commit comments