蜗牛慢慢爬 LeetCode 1.Two Sum [Difficulty: Easy]
题目
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]的更多相关文章
- 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]
题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- 蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]
题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...
- 蜗牛慢慢爬 LeetCode 7. Reverse Integer [Difficulty: Easy]
题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have ...
- 蜗牛慢慢爬 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 ...
- 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 蜗牛慢慢爬 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 ...
- 蜗牛慢慢爬 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 ...
随机推荐
- 【Spark】编程实战之模拟SparkRPC原理实现自定义RPC
1. 什么是RPC RPC(Remote Procedure Call)远程过程调用.在Hadoop和Spark中都使用了PRC,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的 ...
- 修改并编译golang源码
最近为了做Hyperledger Fabric国密改造,涉及到了golang源码的改动.特将操作过程整理如下,以供参考: golang的源码安装其实比较简单,只需运行源码包中的脚本src/all.ba ...
- 20155206 实验一《Java开发环境的熟悉》实验报告
实验内容 使用JDK编译.运行简单的Java程序 使用IDEA 编辑.编译.运行.调试Java程序. 实验代码 `import java.text.DecimalFormat; import java ...
- 20155215 2016-2017-2《Java程序设计》课程总结
20155215 2016-2017-2<Java程序设计>课程总结 一.(按顺序)每周作业链接汇总 预备作业1: 对于JAVA课程本身的期望和理解.以及期望的师生关系是什么样的,自己印象 ...
- 20155230 实验二《Java面向对象程序设计》实验报告
20155230 实验二<Java面向对象程序设计>实验报告 一.单元测试 三种代码 知道了伪代码.产品代码.测试代码的关系和用途,并根据老师的例子,按测试代码调试了产品代码. 值得注意的 ...
- 20155334 实验二 Java面向对象程序设计
实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验要求 完成实验.撰写 ...
- 4709: [Jsoi2011]柠檬
4709: [Jsoi2011]柠檬 https://www.lydsy.com/JudgeOnline/problem.php?id=4709 分析: 决策单调性+栈+二分. 首先挖掘性质:每个段选 ...
- NLP的12条精髓
NLP是神经语言程序学 (Neuro-Linguistic Programming) 的英文缩写.一.没有两个人是一样的 No two persons are the same. 1.没有两个人的人生 ...
- grep 文件内容搜索
比如现在我们要查找在/home/userlan/目录中哪些文件中包含foobar这个关键字 1. 打开终端2. 输入命令 grep -rl "foobar" /home/userl ...
- Linux 安装Redis<单机版>(使用Mac远程访问)
阅读本文需要先阅读安装Redis<准备> redis依赖 yum install gcc-c++ 解压 cd redis压缩包所在目录 tar -xvf redis-4.0.10.tar. ...