MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / twoSum

Method twoSum

Java/two-sum.java:11–28  ·  view source on GitHub ↗
(int[] nums, int target)

Source from the content-addressed store, hash-verified

9*/
10class Solution {
11 public int[] twoSum(int[] nums, int target) {
12
13 Map<Integer, Integer> sumMap = new HashMap<>();
14 int [] result = new int[2];
15
16 for (int i=0; i<nums.length(); i++){
17
18 if(!sumMap.isEmpty() && sumMap.containsKey(nums[i])){
19 result[0] = sumMap.get(nums[i]);
20 result[1] = i;
21
22 return result;
23 }
24 else{
25 sumMap.put(target-nums[i],i);
26 }
27 }
28 }
29}

Callers

nothing calls this directly

Calls 4

lengthMethod · 0.80
isEmptyMethod · 0.45
getMethod · 0.45
putMethod · 0.45

Tested by

no test coverage detected