Skip to content

Commit

Permalink
2sum
Browse files Browse the repository at this point in the history
  • Loading branch information
s50600822 committed Nov 24, 2023
1 parent 1bde45d commit 5f9302c
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public static int[] twoSum(int[] nums, int target) {
java.util.Map<Integer, Integer> map = new java.util.HashMap<>();
for (int i = 0; i< nums.length; i++){
int com = target - nums[i];
if(map.containsKey(com)){
return new int[]{map.get(com),i} ;
}
map.put(nums[i],i);
}
return new int[] {9999,9999};
}

public static void main(String[] args) {
//https://leetcode.com/problems/two-sum/description/
// assert twoSum(new int[]{3,2,4},6 ) == new int[]{1,2} : "{3,2,4} 6 ---> {1,2}";
// assert twoSum(new int[]{3,3},6 ) == new int[]{0,1} : "{3,3} 6 ---> {0,1}";

assert java.util.Arrays.equals(twoSum(new int[]{3,2,4},6 ), new int[]{1,2});
assert java.util.Arrays.equals(twoSum(new int[]{3,3},6), new int[]{0,1});

}
}

0 comments on commit 5f9302c

Please sign in to comment.