TwoSum - nokbeonlab/coding_test GitHub Wiki

Problem

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.


Example: Given nums = [2, 8, 11, 14], target = 16, Because nums[0] + nums[3] = 2 + 14 = 16 return [1, 4].

Code

import java.util.*;

public class T02_TwoSum {
    public static void main(String[] args) {

        T02_TwoSum a = new T02_TwoSum();

        int[] nums = {2, 8, 11, 14};
        int target = 16;
        int[] result = a.sovle(nums, target);
        for(int i : result) {
            System.out.println(i+" ");
        }

    }

    public int[] sovle(int[] nums, int target) {
        int[] result = new int[2];
        Map<Integer, Integer> map = new HashMap<>();

        for(int i=0; i<nums.length; i++) {
            if(map.containsKey(nums[i])) {
                int value = map.get(nums[i]);
                result[0] = value + 1;
                result[1] = i + 1;
            } else {
                map.put(target - nums[i], i);
            }
        }
        return result;
    }
}