Home

1. Two Sum

Detailed explanation coming soon!

// 1. Two Sum

class Solution {
  public int[] twoSum(int[] nums, int target) {
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); // map value -> index

    for(int i = 0; i < nums.length; i++) {
      int buddy = target - nums[i];

      Integer buddyIndex = map.get(buddy);

      if(buddyIndex != null) {
        int[] answer = { buddyIndex, i };
        return answer;
      }

      map.put(nums[i], i);
    }

    return new int[2];
  }
}


Questions? Have a neat solution? Comment below!