【LeetCode】1. 两数之和

题目链接

文章目录

    • Python3
      • [方法一:暴力枚举 ⟮ O ( N 2 ) 、 O ( 1 ) ⟯ \lgroup O(N^2)、O(1)\rgroup ⟮O(N2)、O(1)⟯](#方法一:暴力枚举 ⟮ O ( N 2 ) 、 O ( 1 ) ⟯ \lgroup O(N^2)、O(1)\rgroup ⟮O(N2)、O(1)⟯)
      • [方法二:哈希表 ⟮ O ( N ) ⟯ \lgroup O(N)\rgroup ⟮O(N)⟯](#方法二:哈希表 ⟮ O ( N ) ⟯ \lgroup O(N)\rgroup ⟮O(N)⟯)
    • C++
      • [方法一:暴力枚举 ⟮ O ( N 2 ) 、 O ( 1 ) ⟯ \lgroup O(N^2)、O(1)\rgroup ⟮O(N2)、O(1)⟯](#方法一:暴力枚举 ⟮ O ( N 2 ) 、 O ( 1 ) ⟯ \lgroup O(N^2)、O(1)\rgroup ⟮O(N2)、O(1)⟯)
        • [C++ 的数组 是 { } 形式](#C++ 的数组 是 { } 形式)
      • [方法二:哈希表 ⟮ O ( N ) ⟯ \lgroup O(N)\rgroup ⟮O(N)⟯](#方法二:哈希表 ⟮ O ( N ) ⟯ \lgroup O(N)\rgroup ⟮O(N)⟯)


Python3

方法一:暴力枚举 ⟮ O ( N 2 ) 、 O ( 1 ) ⟯ \lgroup O(N^2)、O(1)\rgroup ⟮O(N2)、O(1)⟯

python 复制代码
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        n = len(nums)
        for i in range(n):
            for j in range(i+1, n):
                if nums[i] + nums[j] == target:
                    return [i, j]

        return []

方法二:哈希表 ⟮ O ( N ) ⟯ \lgroup O(N)\rgroup ⟮O(N)⟯

python 复制代码
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        dic = {}
        for i, num in enumerate(nums):
            if target - num in dic:
                return [dic[target-num], i]
            else:
                dic[num] = i 
        return []

C++

方法一:暴力枚举 ⟮ O ( N 2 ) 、 O ( 1 ) ⟯ \lgroup O(N^2)、O(1)\rgroup ⟮O(N2)、O(1)⟯

cpp 复制代码
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        int n = nums.size();
        for (int i = 0; i < n; ++i){
            for (int j = i + 1; j < n; ++j){
                if (nums[i] + nums[j] == target){
                    return {i, j};   // 怎么是这种形式呢
                }
            }
        }
        return {};
    }
};
C++ 的数组 是 { } 形式

文档, 这样看来,只有 { } 这种形式

方法二:哈希表 ⟮ O ( N ) ⟯ \lgroup O(N)\rgroup ⟮O(N)⟯

cpp 复制代码
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int, int> dic;
        for (int i = 0; i < nums.size(); ++i){
            auto it = dic.find(target - nums[i]); // auto 代替 迭代器类型, 提示编译器自动识别。简化代码
            //这里等效于
            //unordered_map<int, int>::iterator it = dic.find(target - nums[i]);
            if (it != dic.end()){
                return {it->second, i}; // 注意 字典 的 find 函数 会同时返回 键和值 
            }
            else{
                dic[nums[i]] = i;
            }
        }
        return {};        
    }
};
unordered_map::find

unordered_map::find 文档



这样看来用 result 好些,因为不一定是正确答案。

相关推荐
郝学胜_神的一滴1 小时前
CMake 27:缓存变量的特性、语法、类型与实操全解
c++·cmake
To_OC12 小时前
LC 49 字母异位词分组:想到哈希表很简单,选对 key 才是精髓
javascript·算法·leetcode
To_OC2 天前
LC 1 两数之和:面试第一道必考题,暴力解法直接被面试官 pass
javascript·算法·leetcode
博客18002 天前
酷宝的使用方法,超好用的免费界面库,C++、MFC可用
c++·mfc·界面库·库来帮·酷宝
郝学胜_神的一滴2 天前
CMake 026:属性体系精讲、四大作用域全解 & 实战代码落地
c++·cmake
众少成多积小致巨3 天前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++
clint4567 天前
C++进阶(1)——前景提要
c++
夜悊7 天前
C++代码示例:进制数简单生成工具
c++
郝学胜_神的一滴7 天前
CMake 021: IF 条件判据详诠
c++·cmake
_wyt0018 天前
洛谷 B3930 [GESP202312 五级] 烹饪问题 题解
c++·gesp