MCPcopy
hub / github.com/qiyuangong/leetcode / twoSum

Method twoSum

java/001_Two_Sum.java:3–13  ·  view source on GitHub ↗
(int[] nums, int target)

Source from the content-addressed store, hash-verified

1public class Solution {
2 // example in leetcode book
3 public int[] twoSum(int[] nums, int target) {
4 Map<Integer, Integer> map = new HashMap<>();
5 for (int i = 0; i < nums.length; i++) {
6 int x = nums[i];
7 if (map.containsKey(target - x)) {
8 return new int[]{map.get(target - x), i};
9 }
10 map.put(x, i);
11 }
12 throw new IllegalArgumentException("No two sum solution");
13 }
14}

Callers

nothing calls this directly

Calls 3

containsKeyMethod · 0.80
getMethod · 0.45
putMethod · 0.45

Tested by

no test coverage detected