function twoSumBruteForce(nums: number[], target: number): number[] {
for (let i = 0; i < nums.length; i++) {
for (let j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] === target) {
return [i, j];
}
}
}
return [];
}
function twoSumHashMap(nums: number[], target: number): number[] {
const numMap = new Map<number, number>();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (numMap.has(complement)) {
return [numMap.get(complement)!, i];
}
numMap.set(nums[i], i);
}
return [];
}
console.log(twoSumBruteForce([2, 7, 11, 15], 9));
console.log(twoSumBruteForce([3, 2, 4], 6));
console.log(twoSumHashMap([2, 7, 11, 15], 9));
console.log(twoSumHashMap([3, 2, 4], 6));