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. java全栈day06---数组的练习 冒泡排序

    水仙花练习功能实现 * A: 水仙花练习功能实现 * a: 题目分析 * 明确什么样的数就是水仙花数.水仙花数是指一个3位数(100-999之间),其每位数字立方之和等于该3位数本身. 如153 = ...

  2. 20169219《linux内核原理与分析》第九周作业

    网易云课堂学习 可执行程序的装载 可执行程序的产生过程:预处理-----> 编译 ----> 汇编 ----> 链接 以hello.c文件为例进行分析,编译步骤如下 vi hello ...

  3. 安装windows系统备忘

    1.已写入系统镜像的U盘 2.激活工具(同时激活系统及office) 3.如果没有网口或网线,需要准备万能网卡驱动 4.office 5.360 6.输入法 7.微信 8.谷歌浏览器

  4. python(一):作用域

    与c相比,python作用域很奇特. 在Python中变量的作用域是由它在源代码中的位置决定的,这一点与c相似. python只支持4种作用域,即局部作用域,全局作用域,内置作用域,嵌套作用域. 比较 ...

  5. 读取url接口数据

    string url = "http://localhost:8180/city-smscenter/smscenter?cmd=flowsms.queryMobileSmsList& ...

  6. 微信创建带参数二维码,并加上logo

    现在需要创建一个场景二维码,除了基础的微信接口创建外,需要加上小logo,思路如下: 1. 首先根据微信的开发文档创建二维码,获取二维码的url,没啥可说的,按照文档来就好了 获取到的二维码就是这么素 ...

  7. 一键结束port 5037占用

    输入cmd进入dos界面,进入android-sdk-windows\platform-tools目录,执行下面命令启动adb start-server出现下面错误* daemon not runni ...

  8. 理解CNN中的通道 channel

    在深度学习的算法学习中,都会提到 channels 这个概念.在一般的深度学习框架的 conv2d 中,如 tensorflow .mxnet ,channels 都是必填的一个参数. channel ...

  9. VIRT(虚拟内存)RES(常驻内存)和SHR(共享内存)

    VIRT: 1.进程“需要的”虚拟内存大小,包括进程使用的库.代码.数据,以及malloc.new分配的堆空间和分配的栈空间等: 2.假如进程新申请10MB的内存,但实际只使用了1MB,那么它会增长1 ...

  10. loj #2143. 「SHOI2017」组合数问题

    #2143. 「SHOI2017」组合数问题   题目描述 组合数 Cnm\mathrm{C}_n^mC​n​m​​ 表示的是从 nnn 个互不相同的物品中选出 mmm 个物品的方案数.举个例子, 从 ...