【两数之和】有人相爱,有人夜里开车看海,有人leetcode第一题都做不出来

题目:两数之和


给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案,并且你不能使用两次相同的元素。
你可以按任意顺序返回答案。
示例 1:
输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。

示例 2:
输入:nums = [3,2,4], target = 6
输出:[1,2]

示例 3:
输入:nums = [3,3], target = 6
输出:[0,1]
解法:哈希表
哈希表的使用:
我们创建了一个哈希表 hashMap,用于存储已经遍历过的数组元素及其下标。键为数组元素,值为该元素的下标。
遍历数组:
对于数组中的每一个元素 nums[i],首先计算它需要的目标补数 complement = target – nums[i]。
如果这个补数存在于哈希表中,说明之前已经遍历过该数,它可以与当前元素的和等于目标值,因此直接返回它们的下标。
存储当前元素:
如果没有找到合适的补数,则将当前元素及其下标存入哈希表,以便在之后的元素中找到它可能的配对。
返回结果:
根据题目要求,题目保证一定有解,因此代码中没有处理未找到结果的情况。如果有实际需求,也可以在未找到解的情况下返回 nil 或者其他指示错误的值。
package main

import “fmt”

func twoSum(nums []int, target int) []int {
// 创建一个哈希表用于存储数值及其对应的索引
hashMap := make(map[int]int)
// 遍历数组
for i, num := range nums {
// 计算当前数 num 需要的目标补数
complement := target – num
// 如果补数存在于哈希表中,则找到了答案
if index, found := hashMap[complement]; found {
return []int{index, i}
}
// 否则,将当前数和它的索引加入哈希表
hashMap[num] = i
}
// 如果没有找到,则返回空数组(题目保证了必定有解)
return nil
}

func main() {
// 示例 1
nums1 := []int{2, 7, 11, 15}
target1 := 9
result1 := twoSum(nums1, target1)
fmt.Println(result1) // 输出: [0, 1]

// 示例 2
nums2 := []int{3, 2, 4}
target2 := 6
result2 := twoSum(nums2, target2)
fmt.Println(result2) // 输出: [1, 2]

// 示例 3
nums3 := []int{3, 3}
target3 := 6
result3 := twoSum(nums3, target3)
fmt.Println(result3) // 输出: [0, 1]

}

Java实现
import java.util.HashMap;
import java.util.Map;

public class TwoSum {
public static int[] twoSum(int[] nums, int target) {
Map hashMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target – nums[i];
if (hashMap.containsKey(complement)) {
return new int[]{hashMap.get(complement), i};
}
hashMap.put(nums[i], i);
}
return null;
}

public static void main(String[] args) {
    int[] nums = {2, 7, 11, 15};
    int target = 9;
    int[] result = twoSum(nums, target);
    if (result!= null) {
        System.out.println("[" + result[0] + ", " + result[1] + "]");
    }
}

}
php实现
function twoSum($nums, $target) {
$hashMap = [];
foreach ($nums as $i => $num) {
$complement = $target – $num;
if (isset($hashMap[$complement])) {
return [$hashMap[$complement], $i];
}
$hashMap[$num] = $i;
}
return null;
}

$nums = [2, 7, 11, 15];
$target = 9;
$result = twoSum($nums, $target);
if ($result!== null) {
echo “[“. $result[0]. “, “. $result[1]. “]”;
}
C++实现

include

include

include

std::vector twoSum(std::vector& nums, int target) {
std::unordered_map hashMap;
for (int i = 0; i < nums.size(); i++) {
int complement = target – nums[i];
if (hashMap.find(complement)!= hashMap.end()) {
return {hashMap[complement], i};
}
hashMap[nums[i]] = i;
}
return {};
}

int main() {
std::vector nums = {2, 7, 11, 15};
int target = 9;
std::vector result = twoSum(nums, target);
if (!result.empty()) {
std::cout << “[” << result[0] << “, ” << result[1] << “]” << std::endl;
}
return 0;
}
https://leetcode.cn/leetbook/read/top-interview-questions-easy/x2jrse/

声明:文中观点不代表本站立场。本文传送门:https://eyangzhen.com/422791.html

(0)
联系我们
联系我们
分享本页
返回顶部