LeetCode hot 100—两数之和

题目

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标。

你可以假设每种输入只会对应一个答案,并且你不能使用两次相同的元素。

你可以按任意顺序返回答案。

示例

输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
输入:nums = [3,2,4], target = 6
输出:[1,2]
输入:nums = [3,3], target = 6
输出:[0,1]

分析 

暴力枚举

通过两重循环遍历所有可能的元素对,判断其和是否等于目标值。

时间复杂度:O(n^{2})

空间复杂度:O(1)

for (int i = 0; i < nums.size(); i++) {
    for (int j = i + 1; j < nums.size(); j++) {
        if (nums[i] + nums[j] == target) {
            return {i, j};
        }
    }
}

哈希表法

使用哈希表来存储已经遍历过的数组元素的值及其索引,通过哈希表的查找操作可以在常数时间内确定某个数是否存在。

时间复杂度:O(n)

空间复杂度:O(n)

unordered_map<int, int> num_map;
for (int i = 0; i < nums.size(); i++) {
    int complement = target - nums[i];
    if (num_map.find(complement) != num_map.end()) {
        return {num_map[complement], i};
    }
    num_map[nums[i]] = i;
}

知识充电

unordered_map是基于哈希表(hash table)实现的。它使用哈希函数将键映射到数据储存的桶(bucket)中,每个bucket可以储存一个或多个元素,元素是无序存储的。unordered_map在平均情况下,插入、删除和查找操作的时间复杂度为 O(1),但在最坏情况下,如发生大量哈希碰撞,这些操作的时间复杂度可能退化到 O(n)

插入元素

insert()

unordered_map<string, int> umap;
umap.insert({"apple", 1});  // 插入一个键值对
umap.insert(make_pair("banana", 2));  // 另一种插入方式

emplace()

umap.emplace("cherry", 3);  // 在容器中原地构造键值对

查找元素

find()

auto it = umap.find("banana"); //查找指定键是否存在,返回一个迭代器
if (it != umap.end()) {
    cout << "Found banana: " << it->second << endl;
} else {
    cout << "banana not found" << endl;
}

删除元素

erase()

umap.erase("apple");  // 删除键为 "apple" 的元素

auto it = umap.find("banana");
if (it != umap.end()) {
    umap.erase(it);  // 通过迭代器删除元素
}

clear()

umap.clear();  // 删除所有元素

访问元素

operator []

cout << "apple: " << umap["apple"] << endl;  // 输出 apple: 1
cout << "orange: " << umap["orange"] << endl;  // 如果键不存在,则会插入一个新的键值对。插入键 "orange",并初始化为 0

at()

try {
    cout << umap.at("banana") << endl;  // 输出 2
    cout << umap.at("orange") << endl;  // 抛出异常,因为 orange 不存在
} catch (const out_of_range& e) {
    cout << "Exception: " << e.what() << endl;  // 输出 Exception: map::at
}

检查键个数

count()

if (umap.count("banana") > 0) {   //返回值为 0 或 1
    cout << "banana exists" << endl;
} else {
    cout << "banana does not exist" << endl;
}

检查容器状态

empty()

if (umap.empty()) {
    cout << "unordered_map is empty" << endl;
} else {
    cout << "unordered_map is not empty" << endl;
}

size()——返回元素数量

交换容器

swap()

unordered_map<string, int> umap2;
umap2.insert({"grape", 4});

umap.swap(umap2);  // 交换 umap 和 umap2 的内容

桶操作

bucket_count()——返回 unordered_map 中桶的数量

bucket_size()

cout << "Size of bucket 0: " << umap.bucket_size(0) << endl; //返回指定桶中元素的数量

bucket()

cout << "Bucket for key 'banana': " << umap.bucket("banana") << endl; //返回指定键所在的桶的索引

rehash()

umap.rehash(50);  // 重新哈希,确保桶的数量至少为 50

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rigidwill666

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值