1. Two sum
#include <stdbool.h>
#include <stdlib.h>
typedef struct {
int *keys;
int *values;
bool *used;
int capacity;
} HashMap;
static HashMap hashmap_create(int capacity) {
HashMap map;
map.capacity = capacity;
map.keys = calloc(capacity, sizeof(int));
map.values = calloc(capacity, sizeof(int));
map.used = calloc(capacity, sizeof(bool));
return map;
}
static void hashmap_free(HashMap *map) {
free(map->keys);
free(map->values);
free(map->used);
}
static bool hashmap_get(HashMap *map, int key, int *value) {
unsigned int idx = (unsigned int)key % map->capacity;
while (map->used[idx]) {
if (map->keys[idx] == key) {
*value = map->values[idx];
return true;
}
idx = (idx + 1) % map->capacity;
}
return false;
}
static void hashmap_put(HashMap *map, int key, int value) {
unsigned int idx = (unsigned int)key % map->capacity;
while (map->used[idx]) {
idx = (idx + 1) % map->capacity;
}
map->used[idx] = true;
map->keys[idx] = key;
map->values[idx] = value;
}
int *twoSum(int *nums, int numsSize, int target, int *returnSize) {
HashMap seen = hashmap_create(numsSize * 2);
int *result = NULL;
*returnSize = 0;
for (int i = 0; i < numsSize; i++) {
int j;
if (hashmap_get(&seen, nums[i], &j)) {
result = (int *)malloc(2 * sizeof(int));
result[0] = j;
result[1] = i;
*returnSize = 2;
break;
}
hashmap_put(&seen, target - nums[i], i);
}
hashmap_free(&seen);
return result;
}
1_solution1_hashmap.cpp
#include <unordered_map>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> twoSum(vector<int> &nums, int target) {
unordered_map<int, int> map;
for (int i = 0; i < nums.size(); i++) {
if (map.find(nums[i]) != map.end()) {
return {map[nums[i]], i};
} else {
map[target - nums[i]] = i;
}
}
return {};
}
};
1_solution2_map_optimized.cpp
#include <unordered_map>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> twoSum(vector<int> &nums, int target) {
unordered_map<int, int> seen;
seen.reserve(nums.size());
for (int i = 0; i < (int)nums.size(); i++) {
auto it = seen.find(nums[i]);
if (it != seen.end()) {
return {it->second, i};
}
seen[target - nums[i]] = i;
}
return {};
}
};
package main
func twoSum(nums []int, target int) []int {
seen := make(map[int]int)
for i, num := range nums {
if j, ok := seen[num]; ok {
return []int{j, i}
}
seen[target-num] = i
}
return []int{}
}
import java.util.HashMap;
import java.util.Map;
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i])) {
return new int[] { map.get(nums[i]), i };
} else {
map.put(target - nums[i], i);
}
}
return new int[] {};
}
}
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function (nums, target) {
const map = new Map();
for (let i = 0; i < nums.length; i++) {
if (map.has(nums[i])) {
return [map.get(nums[i]), i];
} else {
map.set(target - nums[i], i);
}
}
return [];
};
class Solution:
def twoSum(self, nums: list[int], target: int) -> list[int]:
seen = {}
for i, num in enumerate(nums):
if num in seen:
return [seen[num], i]
else:
seen[target - num] = i
return []
use std::collections::HashMap;
impl Solution {
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
let mut seen: HashMap<i32, i32> = HashMap::new();
for (i, &num) in nums.iter().enumerate() {
if let Some(&j) = seen.get(&num) {
return vec![j, i as i32];
}
seen.insert(target - num, i as i32);
}
vec![]
}
}