1. Two Sum --LeetCode java

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

```

private static int[] getTargetArray(int[] arr, int target) {

if (arr == null || arr.length == 0) return null;

Map<Integer, Integer> map = new HashMap<Integer, Integer>();

int index1 = 0;

int index2 = 0;

for (int i = 0; i < arr.length; i++) {

if (map.containsKey(target - arr[i])) {

index1 = map.get(target - arr[i]);

index2 = i;

int[] targetArr = {index1,index2};

return targetArr;

} else {

map.put(arr[i], i);

}

}

return null;

```

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容