leetcode148two-sum
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
题目描述
给出的数组为 {2, 7, 11, 15},目标值为9
输出 ndex1=1, index2=2
输入
[3,2,4],6
输出
[2,3]
//方法一 暴力
//方法二 C++版本的两遍哈希表(官方题解)
/*
通过以空间换取速度的方式,我们可以将查找时间从 O(n) 降低到 O(1)。
C++版本的哈希表算法采用unordered_map。
*/
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> ans;
unordered_map<int,int> tmpmap;
int length = nums.size();
for(int i = 0;i < length;i++){
tmpmap[nums[i]] = i;
}
for (int i = 0; i < length; i++){
if(tmpmap.count(target - nums[i]) != 0 && tmpmap[target - nums[i]] != i){
//使用count,返回的是被查找元素的个数。如果有,返回1;否则,返回0。
ans.push_back(i);
ans.push_back(tmpmap[target - nums[i]]);
break;
}
}
return ans;
}
//方法三 C++版本的一遍哈希表(官方题解)
/*
事实证明,我们可以一次完成。在进行迭代并将元素插入到表中的同时,我们还会回过头来检查
表中是否已经存在当前元素所对应的目标元素。如果它存在,那我们已经找到了对应解,并立即将其返回。
*/
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> ans;
unordered_map<int,int> tmpmap;
int length = nums.size();
for (int i = 0; i < length; i++){
if(tmpmap.count(nums[i]) != 0){
ans.push_back(tmpmap[nums[i]]);
ans.push_back(i);
break;
}
tmpmap[target - nums[i]] = i;
}
return ans;
}
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
cache = {}
for index,num in enumerate(nums):
another_num = target-num
if another_num in cache:
return [cache[another_num],index]
cache[num]=index
return None
leetcode148two-sum的更多相关文章
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [LeetCode] Sum of Left Leaves 左子叶之和
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
随机推荐
- 【题解】[SDOI2015]星际战争
\(\color{red}{Link}\) \(\text{Solution:}\) 观察到,如果一个时间\(T\)可以完成任务,则\(T+1\)这个时间也可以完成任务. 于是我们可以二分. 为了避免 ...
- CF724G 【Xor-matic Number of the Graph】
题目就不翻译了吧,应该写的很清楚了... 首先 \(,\) 不懂线性基的可以戳这里.知道了线性基\(,\) 但是从来没有写过线性基和图论相结合的\(,\) 可以戳这里. 好\(,\) 点完了这些前置技 ...
- Dubbo部分知识点总结
Dubbo部分 Dubbo工作原理 dubbo工作原理第一层:service层,接口层,给服务提供者和消费者来实现的第二层:config层,配置层,主要是对dubbo进行各种配置的第三层:proxy层 ...
- 多测师讲解自动化 _rf 变量_高级讲师肖sir
rf变量 log 打印全局变量 列表变量: 字典变量: 查看当前工程下的变量 紫色表示变量名有误 设置全局变量 设置列表变量 设置字段变量 关键字书写格式问题
- 多测师讲解python __for 循环___高级讲师肖sir
横向输出 1.遍历字符串 2.遍历列表 3.遍历元组 方法一: 方法二: 方法三: #循环字典:方法一# dict1={"name":"zhihao",&quo ...
- Java 集合看这一篇就够了
大家好,这里是<齐姐聊数据结构>系列之大集合. 话不多说,直接上图: Java 集合,也称作容器,主要是由两大接口 (Interface) 派生出来的: Collection 和 Map ...
- C++虚函数与多继承
虚函数 C++用虚函数实现运行时多态,虚函数的实现是由两个部分组成的,虚函数指针与虚函数表. 虚函数指针(vptr)是指向虚函数表的指针,在一个被实例化的对象中,它总是被存放在该对象的地址首位.而虚函 ...
- pytest文档49-命令行参数--tb的使用
前言 pytest 使用命令行执行用例的时候,有些用例执行失败的时候,屏幕上会出现一大堆的报错内容,不方便快速查看是哪些用例失败. --tb=style 参数可以设置报错的时候回溯打印内容,可以设置参 ...
- 不是计算机专业的,可以转专业甚至转行学IT吗?答案揭晓~
相信有这样疑惑的同学不在少数,随着互联网的快速发展,越来越多的人想要转行到IT行业,可又担心自己的专业不对口,影响将来的发展,那么究竟不是计算机专业的可以转行IT吗? 当然是可以的,其实很多的IT大佬 ...
- 【C语言编程入门】Do you know 函数?不知道了吧,我来告诉你!
☆ 函数 在前面我们已经讲过了一些简单的函数,如程序的主函数main().标准输出函数printf().在C语言中,大多数功能都是依靠函数来实现的.But,你知道什么是函数吗?我猜你肯定不知道. 那么 ...