随笔 - 31  文章 - 0  评论 - 9  阅读 - 41075

Two Sum:给出一个整数数组,返回两个数的下标值,令其和等于一个指定的目标值 #Leetcode

复制代码
// Given nums = [2, 7, 11, 15], target = 9,

// Because nums[0] + nums[1] = 2 + 7 = 9,
// return [0, 1].


#include <iostream>
#include <vector>


int GetIndexByValue(const std::vector<int> &inArr, int value, int startIndex) {
    for (int i = startIndex; i < inArr.size(); ++i) {
        if (value == inArr[i]) {
            return i;
        }
    }

    return -1;
}

std::vector<int> GetIndicesOf2Addons(const std::vector<int> &inArr, int target) {
    for (int i = 0; i < inArr.size(); ++i) {
        int _2ndAddon = target - inArr[i];
        int indexOf2ndAddon = GetIndexByValue(inArr, _2ndAddon, i + 1);
        if (indexOf2ndAddon < 0) {
            std::cout << "Failed to find: " << _2ndAddon << "\n";
            continue;
        }

        return std::vector<int> {i, indexOf2ndAddon};
    }

    return std::vector<int>(0);
}


int main(int argc, char const *argv[]) {
    std::vector<int> nums {
        2, 3, 11, 15, -2
    };
    int target = 4;

    std::vector<int> result = GetIndicesOf2Addons(nums, target);

    if (result.empty()) {
        std::cout << "Failed.\n";
        return -1;
    }

    for (auto i: result) {
        std::cout << i << "\t";
    }
    std::cout << "\n";

    return 0;
}

// g++ two_sum.cpp -std=c++11 && ./a.out
复制代码

 参考,https://cloud.tencent.com/developer/article/1010478,使用hash_map或者unordered_map实现:

复制代码
// Given nums = [2, 7, 11, 15], target = 9,

// Because nums[0] + nums[1] = 2 + 7 = 9,
// return [0, 1].


#include <iostream>
#include <vector>
#include <unordered_map>


class Solution {
public:
    std::vector<int> twoSum(std::vector<int> &numbers, int target) {
        // Key is the number and value is its index in the std::vector.
        std::unordered_map<int, int> hash;

        for (int i = 0; i < numbers.size(); i++) {
            int numberToFind = target - numbers[i];
            // if numberToFind is found in map, return them
            if (hash.find(numberToFind) != hash.end()) {
                return std::vector<int> {i, hash[numberToFind]};
            }

            // number was not found. Put it in the map.
            hash[numbers[i]] = i;
        }

        return std::vector<int>(0);
    }
};


int main(int argc, char const *argv[]) {
    std::vector<int> nums {
        2, 3, 11, 15
    };
    int target = 0;

    Solution sol;
    std::vector<int> res = sol.twoSum(nums, target);
    if (res.empty()) {
        std::cout << "Failed.\n";
        return -1;
    }

    for (auto i: res) {
        std::cout << i << "\t";
    }
    std::cout << "\n";

    return 0;
}

// g++ two_sum.cpp -std=c++11 && ./a.out
复制代码

算法本身遍历一次,花费了 O(n) 的时间复杂度,遍历过程中的 find() 方法本身花费 O(log n),所以该算法总时间复杂度为 O(nlog n)。

posted on   _bob  阅读(225)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示