Level:

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

思路分析:

​  给定一个目标值,要返回数组中两个数之和等于该目标值的两数下标,我们可以借助map来实现,将元素作为键,下标作为值存放进map,我们将目标值记为target,当遍历到数组中某个元素num时,我们查看map中是否存有target-num这个键,如果存在那么我们就找到了满足要求的两个数,如果不存在,继续向下遍历,直到找到答案。

代码:

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

1.Tow Sum(两数和)的更多相关文章

  1. LeetCode刷题 1. Two Sum 两数之和 详解 C++语言实现 java语言实现

    1. Two Sum 两数之和 Given an array of integers, return indices of the two numbers such that they add up ...

  2. 【LeetCode】1. Two Sum 两数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:two sum, 两数之和,题解,leetcode, 力 ...

  3. [LeetCode] 1. Two Sum 两数和

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  4. [LeetCode] 1. Two Sum 两数之和

    Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...

  5. [LeetCode] Two Sum 两数之和

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  6. [LeetCode] 1.Two Sum 两数之和分析以及实现 (golang)

    题目描述: /* Given an array of integers, return indices of the two numbers such that they add up to a sp ...

  7. [LeetCode]1.Two Sum 两数之和&&第一次刷题感想

    ---恢复内容开始--- 参考博客: https://www.cnblogs.com/grandyang/p/4130379.html https://blog.csdn.net/weixin_387 ...

  8. [LintCode] Two Sum 两数之和

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  9. [leetcode]1. Two Sum两数之和

    Given an array of integers, return indices  of the two numbers such that they add up to a specific t ...

随机推荐

  1. Android添加Menu菜单

    在安卓中添加Menu菜单十分简单. 步骤: 1.在menu文件夹中的main.xml文件中添加要添加的项目. <menu xmlns:android="http://schemas.a ...

  2. 向linux内核增加一个系统调用-1

    验证编辑编译内核的流程,并增加新的系统调用 注意:需要/目录至少10GB空间,/boot目录500MB空间 下载内核并解压 kernel下载 百度云搬运 密码: qc8b 进入 /usr/src目录 ...

  3. solr通过http请求搜索

    请求搜索必要的条件是:设置搜索条件params 设置 1.简单条件 SolrParams params = new SolrQuery("name:小飞鸟 AND  id:1520" ...

  4. 从公交塞车,看C#多线程问题(转)

    好久没写博客了,可能是因为最近工作太过于压抑的原因吧!有点颓废了.... 而且公司距离住处要坐公交将近40--50分钟(各个原因,纠结中ing...),提前一个半小时起床,居然还能迟到!因为距离公司前 ...

  5. 列表控件JList的使用

    --------------siwuxie095                             工程名:TestUI 包名:com.siwuxie095.ui 类名:TestList.jav ...

  6. HBase 协处理器统计行数

    环境:cdh5.1.0 启用协处理器方法1. 启用协处理器 Aggregation(Enable Coprocessor Aggregation) 我们有两个方法:1.启动全局aggregation, ...

  7. Android 菜单之子菜单SubMenu

    子菜单就是在点击了菜单中的选项后弹出的要对菜单中选项操作的菜单           他的操作与之前的两种类型的菜单操作差不多 动态添加 @Override public boolean onCreat ...

  8. Luogu 4281 [AHOI2008]紧急集合 / 聚会

    BZOJ 1832 写起来很放松的题. 首先发现三个点在树上一共只有$3$种形态,大概长这样: 这种情况下显然走到三个点的$lca$最优. 这种情况下走到中间那个点最优. 这种情况下走到$2$最优. ...

  9. scala中枚举

    scala没有从语法的角度来支持枚举,而是通过定义了一个接口Enumeration来支持的 object ExecutorState extends Enumeration{ type Executo ...

  10. WordCount 编码与测试

    word count github 项目地址:https://github.com/liuqiang666/wordCount PSP表格 PSP2.1  PSP阶段  预估耗时(小时)  实际耗时( ...