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. display:none vs visibility:hidden

    [display:none vs visibility:hidden] 设置元素的display为none是最常用的隐藏元素的方法. 1 .hide { 2 display:none; 3 } 将元素 ...

  2. new.target

    [new.target] The new.target property lets you detect whether a function or constructor was called us ...

  3. 六:python 对象类型详解二:字符串(下)

    一:字符串方法: 方法就是与特定对象相关联在一起的函数.从技术的角度来讲,它们是附属于对象的属性,而这些属性不过是些可调用的函数罢了.Python 首先读取对象方法,然后调用它,传递参数.如果一个方法 ...

  4. VM 端口映射问题

    环境: 宿主机:WIN 10 --192.168.9.87 虚拟机:CentOS ---192.168.255.129 联通模式:NAT模式 一.VM做端口映射 1.为了方便管理,设置虚拟机为静态IP ...

  5. Python+Selenium学习--浏览器设置

    场景 设置浏览器窗口的大小有下面两个比较常见的用途: 在统一的浏览器大小下运行用例,可以比较容易的跟一些基于图像比对的工具进行结合,提升测试的灵活性及普遍适用性.比如可以跟sikuli结合,使用sik ...

  6. 学习Junit资料

    以下是找到的一些有用的学习资料,先收藏了 http://www.blogjava.net/jiangshachina/archive/2011/12/14/366289.html http://www ...

  7. TZOJ 3305 Hero In Maze II(深搜)

    描述 500年前,Jesse是我国最卓越的剑客.他英俊潇洒,而且机智过人^_^.突然有一天,Jesse心爱的公主被魔王困在了一个巨大的迷宫中.Jesse听说这个消息已经是两天以后了,他急忙赶到迷宫,开 ...

  8. 创建文件指针数组c++

    #include<fstream>using namespace std; void main(){ for (int i=0; i<=1; i++) { char szName[1 ...

  9. eclipse启动出现Could not read metadata for ……

    Could not read metadata for …… E:\workspace\.metadata\.plugins\org.eclipse.core.resources\.projects\ ...

  10. RabbitMQ 参数们的Power “续”

      参数中的 arguments 之前讲参数的一些作用的时候,忽略了最后一个字典类型的参数,因为这个参数是大有文章的,值得单独进出来说道说道. 这时,就不得不打开我们的 Web UI管理系统了,可以看 ...