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 ...
随机推荐
- DORIS系统概述
DORIS(Doppler Orbitography and Radio-positioning Integrated by Satellite)(多普勒轨道学与无线电定位集成卫星),它是由法国Cne ...
- STM32F103C8T6-CubeMx串口收发程序详细设计与测试(2)——程序规划、代码编写及测试
摘要:演示程序的功能:通过中断接收串口数据,在1750us时间内没有收到新的字节的话,将收到的数据原封不动地发送出去,以测试串口的完整收发流程.对使用到的函数进行了说明,阐述了各个函数的调用顺序和调用 ...
- 达梦产品技术支持培训-day2-DM8常用SQL
(本文只作为随笔或个人笔记,非官方文档,请勿作他用,谢谢) DM8数据库的SQL兼容性很高,和Oracle差距不大,以下是个人认为比较关键的部分. 1.关键动词 create --新建 drop -- ...
- 跨时代的MySQL8.0新特性解读
目录 MySQL发展历程 MySQL8.0新特性 秒级加列 性能提升 文档数据库 SQL增强 共用表表达式(CTEs) 不可见索引(Invisible Indexes) 降序索引(Descending ...
- 2020 CSP-J 初赛答案及解析
部分咕咕咕的明天一定 单项选择 A A D 解析 : 与z的都是假 C 解析 : $ \frac{2048\times1024\times32}{8\times1024\times1024}=8$ C ...
- 重装Windows系统 入门详解 - 基础教程
重装Windows系统 入门详解 - 基础教程 JERRY_Z. ~ 2020 / 10 / 13 转载请注明出处!️ 目录 重装Windows系统 入门详解 - 基础教程 一.说明 二.具体步骤 ( ...
- 多测师讲解jmeter _安装和配置环境(00)_高级讲师肖sir
1.下载jmeter包,我们已经下载了有现成的: 2.安装jjdk默认安装或自定义安装 默认安装的路径: 如下图 3.第三步:安装完成后配置JDK的环境变量 位置:计算机→属性→高级系统设置→高级→ ...
- Mac zsh中所有命令失效
参考文章 https://blog.csdn.net/hujincai_55/article/details/95680245?utm_medium=distribute.pc_relevant.no ...
- docker系统化学习图文+视频教程
1.背景 博客对应的视频课程: 9.9元在线学习:https://study.163.com/course/courseMain.htm?share=2&shareId=40000000033 ...
- 第五章 Linux操作系统关机、重启、注销及其查看ip命令
一.更新系统时间与网络时间同步 1. 安装ntpdate工具 # yum -y install ntp ntpdate 2. 设置系统时间与网络时间同步 # ntpdate cn.pool.ntp ...