【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 好些,因为不一定是正确答案。

相关推荐
blasit12 小时前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
肆忆_2 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星2 天前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛4 天前
delete又未完全delete
c++
端平入洛4 天前
auto有时不auto
c++
琢磨先生David5 天前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
哇哈哈20215 天前
信号量和信号
linux·c++
多恩Stone5 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马5 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝5 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode