1. 两数之和-LeetCode(C++)

1. 两数之和

2024.9.11

题目

给定一个整数数组 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]
题解

将数组nums的值和它对应的索引存入哈希表作为键值对,利用哈希表查询时间复杂度为O(1),查询nums[i]是否存在。

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        std::unordered_map<int,int> num_map;
        for(int i = 0;i<nums.size();++i){
            int second = target - nums[i];
            if(num_map.find(second) != num_map.end()){
                //如果存在
                return {num_map[second], i};
            }
            num_map[nums[i]] = i; // 将当前元素及其索引存入哈希表
        }
        return {};
    }
};
补充——哈希表

哈希表(Hash Table),也称为散列表,是一种通过哈希函数将键(key)映射到表中一个位置以便快速访问记录的数据结构。哈希表的主要特征包括:

  1. 键值对存储:哈希表存储数据的方式是键值对(key-value pairs)。每个键映射到一个值,键是唯一的。
  2. 键的唯一性:在哈希表中,键是唯一的,不能有重复的键。如果尝试插入一个已经存在的键,通常会更新该键对应的值,而不是创建一个新的条目。
  3. 值的重复:虽然键不能重复,但值可以是重复的。不同的键可以映射到相同的值。
  4. 快速访问:哈希表通过哈希函数将键转换为表中的一个索引,从而实现快速访问。

在C++中,哈希表的基本操作如下:

#include <iostream>
#include <unordered_map>
#include <string>

int main() {
    // 创建一个 unordered_map 实例
    std::unordered_map<std::string, int> umap;

    // 插入键值对
    umap["apple"] = 3;
    umap["banana"] = 5;
    umap["cherry"] = 7;

    // 访问元素
    std::cout << "Number of apples: " << umap["apple"] << std::endl; // 输出 3

    // 检查键是否存在
    if (umap.find("banana") != umap.end()) {
        std::cout << "Bananas are present." << std::endl;
    } else {
        std::cout << "Bananas are not present." << std::endl;
    }

    // 删除元素
    umap.erase("cherry");

    // 遍历 unordered_map
    for (const auto& pair : umap) {
        std::cout << pair.first << " => " << pair.second << std::endl;
    }

    // 获取大小
    std::cout << "Size of umap: " << umap.size() << std::endl;

    // 清空 unordered_map
    umap.clear();

    return 0;
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是电话号码的字母组合问题的 C++ 代码实现: ``` class Solution { public: vector<string> letterCombinations(string digits) { unordered_map<char, string> mapping = { {'2', "abc"}, {'3', "def"}, {'4', "ghi"}, {'5', "jkl"}, {'6', "mno"}, {'7', "pqrs"}, {'8', "tuv"}, {'9', "wxyz"} }; vector<string> res; if (digits.empty()) { return res; } string combination; backtrack(res, combination, digits, 0, mapping); return res; } void backtrack(vector<string>& res, string& combination, string& digits, int index, unordered_map<char, string>& mapping) { if (index == digits.size()) { res.push_back(combination); return; } for (char c : mapping[digits[index]]) { combination.push_back(c); backtrack(res, combination, digits, index + 1, mapping); combination.pop_back(); } } }; ``` 其中 `letterCombinations` 函数用来生成所有的字母组合,`backtrack` 函数用来进行回溯操作。在 `letterCombinations` 函数中,首先根据数字字符和字母的映射关系创建了一个 `unordered_map` 对象 `mapping`。然后定义了一个空字符串 `combination` 和一个空 vector `res` 来保存最终结果。最后调用了 `backtrack` 函数来生成所有的字母组合。在 `backtrack` 函数中,首先判断是否达到了数字字符串的末尾,如果是,则将当前的 `combination` 字符串保存到 `res` 中。否则,遍历当前数字字符所能表示的所有字母,依次加入到 `combination` 字符串中,然后递归调用 `backtrack` 函数,添加下一个数字字符所能表示的字母。递归完成后,需要将 `combination` 字符串还原到上一个状态,以便进行下一次回溯。最终返回 `res` 数组即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值