Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

Idea: map to store index

Note. 不要忘记加元素进去Map, don't forget o add element to update map.

Time complexity: O(n)

Space complexity: O(n)

 class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> record = new HashMap<>();
for(int i = 0; i < nums.length; ++i) {
Integer index = record.get(target - nums[i]);
if(index != null) {
return new int[] {index, i};
}
record.put(nums[i], i);
} return new int[0];
}
}

python

 class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
record = {}
for i in range(len(nums)):
if target - nums[i] in record:
return [record[target-nums[i]], i]
record[nums[i]] = i
return []

Two Sum LT1的更多相关文章

  1. Two Sum IV - Input is a BST LT653

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

  2. Two Sum III - Data structure design LT170

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  3. zoj2112 树状数组+主席树 区间动第k大

    Dynamic Rankings Time Limit: 10000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Subm ...

  4. Pairs of Songs With Total Durations Divisible by 60 LT1010

    In a list of songs, the i-th song has a duration of time[i] seconds. Return the number of pairs of s ...

  5. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  6. 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 ...

  7. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  8. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  9. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

随机推荐

  1. 常用的jquerymobil 站点

    http://www.jqmapi.com/api1.2/ Jquery Mobile 中文API站 https://codiqa.com/demo   jquerymobil UI编辑器 https ...

  2. mysqlli

    ./configure --with-mysql=/usr/bin/mysql_config \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd ht ...

  3. zabbix监测公网IP的客户端主机

    未经测试 如果server端是内网的主机,需要注意:防火墙.端口映射 再用zabbix服务器去Telnet客户机的10050端口,然后在客户机中查看10050被什么ip访问了,拿到这个ip之后,加到之 ...

  4. STL::forward_list

    forward_list(c++11): 内部是一个单链表的实现:但是为了效率的考虑,故意没有 size 这个内置函数. Constructor 六种构造方式default; fill; range; ...

  5. 斐波那契数列(python)

    题目描述 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0). n<=39 # -*- coding:utf-8 -*- class Solut ...

  6. word2vec详解与实战

    有那么一句话 不懂word2vec,就别说自己是研究人工智能->机器学习->自然语言处理(NLP)->文本挖掘的 所以接下来我就从头至尾的详细讲解一下word2vec这个东西. 简要 ...

  7. uniquefu Python+Selenium学习--select

    场景 在处理下拉框(select)的时候selenium给我们提供了一系列的便捷方法,我们只需要使用selenium.webdriver.support.select.Select类来稍微封装一下就好 ...

  8. 客户端无法重新使用 SPID 为 63 的会话,该会话已被重置用于连接

    客户端无法重新使用 SPID 为 %d 的会话,该会话已被重置用于连接池.失败 ID 为 %d. 此错误可能是由于先前的操作失败引起的.请查看错误日志,找出在显示此错误消息之前刚发生的失败操作. 20 ...

  9. mysql中的 随机字符串的生成

    方法1. SELECT SUBSTRING(MD5(RAND()),FLOOR(RAND()*26)+1,6) AS rand_str; 上诉示例产生的是:6位长度的随机字符串. 函数解释: rand ...

  10. subprocess.Popen在win10下会有异常

    win10运行下 会报740错误 查了下搜索结果 是uac问题 但uac已经是关闭状态 后直接使用os.popen进行替换 运行ok