题目

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

翻译

给定一个整形数组和一个整数target,返回2个元素的下标,它们满足相加的和为target。

你可以假定每个输入,都会恰好有一个满足条件的返回结果。

Hints

Related Topics: Array, Hash Table

如果简单地想O(n^2)的算法很容易实现

但是可以利用Hash Table来实现O(n)的算法

代码

Java

class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> mymap = new HashMap<>();
for(int i=0;i<nums.length;i++){
Integer index = mymap.get(target-nums[i]);
if(index==null){
mymap.put(nums[i],i);
}else{
return new int[]{i,index};
}
}
return new int[]{0,0};
}
} //solution from discuss
class Solution {
public int[] twoSum(int[] numbers, int target) {
int[] result = new int[2];
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < numbers.length; i++) {
if (map.containsKey(target - numbers[i])) {
result[1] = i + 1;
result[0] = map.get(target - numbers[i]);
return result;
}
map.put(numbers[i], i + 1);
}
return result;
}
}

Python

class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
mymap = {}
for i in range(len(nums)):
if nums[i] in mymap:
return [mymap[nums[i]],i]
else:
mymap[target-nums[i]] = i
return [0,0]

蜗牛慢慢爬 LeetCode 1.Two Sum [Difficulty: Easy]的更多相关文章

  1. 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]

    题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  2. 蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]

    题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...

  3. 蜗牛慢慢爬 LeetCode 7. Reverse Integer [Difficulty: Easy]

    题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have ...

  4. 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]

    题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  5. 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]

    题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  6. 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  7. 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]

    题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  8. 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]

    题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...

  9. 蜗牛慢慢爬 LeetCode 15. 3Sum [Difficulty: Medium]

    题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...

随机推荐

  1. 关于LP64,ILP64,LLP64,ILP32,LP32字长(数据)模型

    太长不看: 1.32位Windows和类Unix使用ILP32字长模型,64位Windows使用ILP64模型,64位类Unix使用LP64字长模型. 2.根据1,long在32位和64位Window ...

  2. 【C】对指针表达式的个人总结与思考

    本文内容参考<c 和 指针>. 声明:本博文只为那些能沉得住气,认真研究,探索真知的人参考,浮躁的人请离开,因为看不懂. 感觉以前学c的时候,学的指针真是白学了,今天看到这个内容,困惑后, ...

  3. 20155207 2006-2007-2 《Java程序设计》第4周学习总结

    20155207 2006-2007-2 <Java程序设计>第4周学习总结 教材学习内容总结 ISP原则:一个类对另一个类的依赖应该限制在最小化的接口上. OCP原则:软件构成(类,模块 ...

  4. c++ 时间函数和结构化数据

     time和localtime  数据结构概念  struct关键字  认识数据结构  自定义结构 例:获取当前系统日期和时间;(代码例子) 一.函数: time 函数time()返回的是当 ...

  5. .NET Core中使用RabbitMQ正确方式

    .NET Core中使用RabbitMQ正确方式 首先甩官网:http://www.rabbitmq.com/ 然后是.NET Client链接:http://www.rabbitmq.com/dot ...

  6. 《手把手教你学DSP-基于TMS320F28335》书中的错误

    1. 在书的345页,这种字符串写法是错误的,char *msg. 2. 估计张卿杰可能是个学着.书的风格感觉就是翻译的PDF文档.

  7. Linux学习(一)------CentOs安装mysql5.5 数据库

    具体方法和步骤如下所示: 1.第一步就是看linu是否安装了mysql,经过rpm -qa|grep mysql查看到centos下安装了mysql5.1,那就开始卸载咯 2.接下来就是卸载mysql ...

  8. fastdfs+nginx+image_filter安装与生成缩略图

    fastdfs简介 类似google FS的一个轻量级分布式文件系统,纯C实现,支持linux.FreeBSD等UNIX系统: 只能通过API访问,不支持POXIS: 文件不分块存储,上传的文件和OS ...

  9. 785. Is Graph Bipartite?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  10. 列出连通集(mooc)

    给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点. 输入格式: 输入第1 ...