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

相关推荐
hi0_627 分钟前
03 数组 VS 链表
java·数据结构·c++·笔记·算法·链表
碧海蓝天20221 小时前
C++法则21:避免将#include放在命名空间内部。
开发语言·c++
CodeWithMe1 小时前
【读书笔记】《C++ Software Design》第一章《The Art of Software Design》
开发语言·c++
Tanecious.3 小时前
C++--红黑树
开发语言·c++
??tobenewyorker3 小时前
力扣打卡第23天 二叉搜索树中的众数
数据结构·算法·leetcode
贝塔西塔3 小时前
一文读懂动态规划:多种经典问题和思路
算法·leetcode·动态规划
tanyongxi665 小时前
C++ Map 和 Set 详解:从原理到实战应用
开发语言·c++
飒飒真编程5 小时前
C++类模板继承部分知识及测试代码
开发语言·c++·算法
呆呆的小鳄鱼6 小时前
leetcode:HJ18 识别有效的IP地址和掩码并进行分类统计[华为机考][字符串]
算法·leetcode·华为
救赎小恶魔7 小时前
C++11的整理笔记
c++·笔记