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

相关推荐
练习时长一年17 分钟前
LeetCode热题100(二叉树的最大路径和)
算法·leetcode·职场和发展
王老师青少年编程7 小时前
信奥赛C++提高组csp-s之搜索进阶(搜索剪枝案例实践1)
c++·csp·高频考点·信奥赛·提高组·搜索剪枝·小木棍
啦哈拉哈10 小时前
Leetcode题解记录-hot100(81-100)
算法·leetcode·职场和发展
王老师青少年编程10 小时前
信奥赛C++提高组csp-s之搜索进阶(搜索剪枝核心思想 )
c++·dfs·csp·信奥赛·搜索剪枝·搜索优化
一拳一个呆瓜10 小时前
【STL】使用 C++ 标准库标头
c++·stl
王老师青少年编程10 小时前
信奥赛C++提高组csp-s之搜索进阶(搜索剪枝案例实践2)
c++·信奥赛·csp-s·提高组·搜索剪枝·生日蛋糕·最优性剪枝
c++之路11 小时前
C++ 设计模式全总结
java·c++·设计模式
c2385611 小时前
c/c++中的多态(上)
开发语言·c++
彷徨而立11 小时前
【C++】介绍 std::ifstream 输入文件流
开发语言·c++
MC皮蛋侠客11 小时前
C++17 多线程系列(十):多线程性能优化——从测量到调优
c++·多线程