Reviewing LeetCode's first problem: 1. Two Sum.

2023-07-01 14:59:22
This beginning is always today. Resorting to solve the first problem on Leetcode named "1. Two Sum".

Example

1. Two Sum

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Answer

Hash Map / Hash Table

var twoSum = function(nums, target) {
  const n = nums.length, map = new Map
  for (let i = 0; i < n; i++) {
    if (map.has(nums[i])) return [map.get(nums[i]), i]
    map.set(target - nums[i], i)
  }
};
function twoSum(nums: number[], target: number): number[] {
  const n = nums.length, map = new Map
  for (let i  = 0; i < n; i++) {
    if (map.has(nums[i])) return [map.get(nums[i]), i]
    map.set(target - nums[i], i)
  }
};
func twoSum(nums []int, target int) []int {
  m := map[int]int{}
  for i, num := range nums {
    if _, ok := m[num]; ok {
      return []int{m[num], i}
    }
    m[target - num] = i
  }
  return []int{}
}
class Solution {
  function twoSum($nums, $target) {
    $map = array();
    foreach ($nums as $i => $num) {
      if ($map[$num] !== null) return [$map[$num], $i];
      $map[$target - $num] = $i; 
    }
  }
}
class Solution {
  public int[] twoSum(int[] nums, int target) {
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    int n = nums.length;
    for (int i = 0; i < n; i++) {
      if (map.containsKey(nums[i])) return new int[]{map.get(nums[i]), i};
      map.put(target - nums[i], i);
    }
    return new int[]{};
  }
}
public class Solution {
  public int[] TwoSum(int[] nums, int target) {
    Dictionary<int, int> map = new Dictionary<int, int>();
    int n = nums.Length;
    for (int i = 0; i < n; i++) {
      if (map.ContainsKey(nums[i])) return new int[]{map[nums[i]], i};
      map[target - nums[i]] = i;
    }
    return new int[]{};
  }
}
class Solution {
public:
  vector<int> twoSum(vector<int>& nums, int target) {
    unordered_map<int, int> map;
    int n = nums.size();
    vector<int> ans(2, 0);
    for (int i = 0; i < n; i++) {
      if (map.count(nums[i]) == 1) {
        ans[0] = map[nums[i]], ans[1] = i;
        return ans;
      }
      map.emplace(target - nums[i], i);
    }
    return ans;
  }
};
typedef struct {
  int key;
  int val;
  UT_hash_handle hh;
} HashItem;
void freeMap(HashItem** map) {
  HashItem *cur, *tmp;
  HASH_ITER(hh, *map, cur, tmp) {
    HASH_DEL(*map, cur);
    free(cur);
  }
}
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
  HashItem* map = NULL;
  int* ans = malloc(sizeof(int) * 2);
  *returnSize = 2;
  for (int i = 0; i < numsSize; i++) {
    HashItem* pEntry = NULL;
    HASH_FIND_INT(map, &nums[i], pEntry);
    if (pEntry != NULL) {
      ans[0] = pEntry->val;
      ans[1] = i;
      freeMap(&map);
      return ans;
    }
    pEntry = malloc(sizeof(HashItem));
    pEntry->key = target - nums[i];
    pEntry->val = i;
    HASH_ADD_INT(map, key, pEntry);
  }
  freeMap(&map);
  return ans;
}
class Solution:
  def twoSum(self, nums: List[int], target: int) -> List[int]:
    m = dict()
    for i, num in enumerate(nums):
      if num in m: return [m[num], i]
      m[target - num] = i