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].
详见:https://leetcode.com/problems/two-sum/description/

给定一个整数数组,找出其中两个数满足相加等于你指定的目标数字。

要求:这个函数twoSum必须要返回能够相加等于目标数字的两个数的索引,且index1必须要小于index2。

Java实现:

暴力解:

class Solution {
public int[] twoSum(int[] nums, int target) {
int[] res=new int[2];
for(int i=0;i<nums.length;++i){
for(int j=i+1;j<nums.length;++j){
if(nums[i]+nums[j]==target){
res[0]=i;
res[1]=j;
}
}
}
return res;
}
}

Map:空间换时间

class Solution {
public int[] twoSum(int[] nums, int target) {
int[] res=new int[2];
Map<Integer,Integer> map=new HashMap<Integer,Integer>();
for(int i=0;i<nums.length;++i){
if(map.containsKey(target-nums[i])){
res[0]=map.get(target-nums[i]);
res[1]=i;
return res;
}
map.put(nums[i],i);
}
return res;
}
}

python:

方法一:

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

方法二:

class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
if target-nums[i] in nums and nums.index(target-nums[i])!=i:
return nums.index(target-nums[i]),i
return -1,-1

golang:

方法一:

func twoSum(nums []int, target int) []int {
for i:=0;i<len(nums);i++{
for j:=i+1;j<len(nums);j++{
if nums[i]+nums[j]==target{
return []int{i,j}
}
}
}
return []int{}
}

方法二:

func twoSum(nums []int, target int) []int {
hash:=make(map[int]int)
for i,v:=range nums{
j,ok:=hash[target-v]
if ok{
return []int{i,j}
}
hash[v]=i
}
return []int{}
}

001 Two Sum 两个数的和为目标数字的更多相关文章

  1. 015 3Sum 三个数的和为目标数字

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

  2. LeetCode 算法题解 js 版 (001 Two Sum)

    LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...

  3. js 数组里面任意两个数的和与目标值

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. 2016网易实习生编程题:数组中两个数的和等于sum

    题目 找出数组中两个数的和等于sum的这两个数 解题 这个题目做过很多次了,利用HashMap,key为 sum-A[i] value为 i 当 加入HashMap时候A[i] 已经存在map中,ge ...

  5. 牛客网2016.4.11(两个数相加为sum/计数一个int型的二进制有多少个1/二叉树是否左右对称)

    求最小的两个数相加为sum //求最小的两个数相加为sum public ArrayList<Integer> FindNumbersWithSum(int [] array,int su ...

  6. shell实现两个数的相加

    刚开始的时候写,一直写不对:看似简单的功能,但是一定要小心:函数的定义: funciton functionName {.....}在functionName和{之间一定有空格啊! 我就是没加空格,就 ...

  7. Hard 不用+号实现两个数之和 @CareerCup

    例子: 759+674 1)不考虑进位:   323 2)只考虑进位:1110 3)两者之和:1433 递归求解c package Hard; /** * Write a function that ...

  8. 【剑指offer】和为定值的两个数

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/24933341 题目描写叙述: 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的 ...

  9. 【剑指offer学习】求和为定值的两个数(拓展)

    接着上面一篇文章: http://blog.csdn.net/u013476464/article/details/40651451 接下来我们拓展一下题目,如果数组是乱序的,并且规定数组中的元素所有 ...

随机推荐

  1. JavaWeb_增强for循环

    引入增强for循环的原因:在JDK5以前的版本中,遍历数组或集合中的元素,需要先获得数组的长度或集合的迭代器,比较麻烦. JDK5中定义了一种新的语法----增强for循环,以简化此类操作.增强for ...

  2. UCOSIII五种状态

    休眠态:未用OSTaskCreate创建任务,不受UCOS管理 就绪态:在就绪表中已经登记,等待获取CPU使用权 运行态:已经获取CPU使用权并运行的任务 等待态:暂时让出CPU使用权,等待某一事件触 ...

  3. xml知识点

    XML 被设计用来传输和存储数据. HTML 被设计用来显示数据.应该掌握的基础知识:在您继续学习之前,需要对以下知识有基本的了解: HTML / XHTML JavaScript 如果您希望首先学习 ...

  4. 批量更改某一目录之下所有文件名 Ver2

    前一篇<批量更改某一目录之下所有文件名>只是批量修改所有子目录下的文件名.Insus.NET重构了它.能让它修改所有子目录名和子目录下的文件名.就是分别迭代,目录迭代目录,文件迭代文件. ...

  5. tomcat的日志文件权限与启动用户的权限不一致

    用户work的文件权限(umask=0002)为 u=rwx,g=rwx,o=rx 但是tomcat的日志文件的权限却是:为什么会不一样呢? 这是因为tomcat在启动(catalina.sh)时会重 ...

  6. 2019-RHCSA-红帽题库(稳定)

    RHCSA考题 Hostname:station.rhce.ccIP address:192.168.122.100Netmask:255.255.255.0Gateway:192.168.122.1 ...

  7. JavaScript——原生js实现瀑布流

    瀑布流介绍及实现原理: 瀑布流是一种页面布局,页面上也有多等宽的块(块就页面内容),每一块都是绝对定位(absolute),每个块排列的方式如下:寻找现在高度最小的列,把该块定位到该列下方.需要知道, ...

  8. Oracle复制表

    Oracle复制表分为只复制表结构或者结构和数据均复制两种: 只复制表结构 create table newTableName as select * from oldTableName where ...

  9. TensorFlow车辆检测

    1.先在UIUC Image Database for Car Detection下载训练数据集. 下载地址:http://cogcomp.org/Data/Car/ 下载解压之后文件目录如图所示,这 ...

  10. Lyft Level 5 Challenge 2018 - Final Round (Open Div. 2) B 1075B (思维)

    B. Taxi drivers and Lyft time limit per test 1 second memory limit per test 256 megabytes input stan ...