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. 2015.3.31不使用debug/X86文件夹方式解决64/32位问题

    传统方法:在解决方案-配置管理器-新建X86平台可以解决32位兼容问题,但是Debug目录下会多出X86文件夹.不方便 另一种方法:在项目名称-属性-生成-目标平台-x86也能解决问题,而且不出现X8 ...

  2. js实现导航栏的吸顶操作

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  3. C++知识点总结(四)——面向对象的编程细节总结

    1.空类的默认函数 一般情况下,对于任意一个类A,如果程序员不显示的声明和定义上述函数,C++编译器将会自动的为A产生4个public inline(公有.内联)的默认函数,这4个函数最常见的形式为: ...

  4. Codeforces 1110D Jongmah (DP)

    题意:你有n个数字,范围[1, m],你可以选择其中的三个数字构成一个三元组,但是这三个数字必须是连续的或者相同的,每个数字只能用一次,问这n个数字最多构成多少个三元组? 解析:首先我们容易发现,我们 ...

  5. input标签name、value与id属性

    id是唯一标识符,不允许有重复值可以通过它的值来获得对应的html标签对象.相当于人的身份证具有唯一性 name:控件的名字,一个控件是否设置它的 name 属性是不会影响到这个网页的功能实现的.但是 ...

  6. Linux-shell实现阳历转农历(序)

    好些天没有登陆邮箱,前几天上班打开一看垃圾箱中有一封邮件让我好激动,还是国外友人的英文邮件.^_^大概内容是我早些时候写的一个阳历转农历的shell小程序,他在用的时候发现了bug,但是这个bug我在 ...

  7. Sql Server 2008 Management studio安装教程

    Sql Server 2008 Management studio安装教程     以下介绍Visual Studio 2010已安装后,sql server 2008 management stud ...

  8. 数据结构 queue

    问题描述 t 个团队在餐厅前准备排队. 他们的排队规则是:初始队伍为空.一个人要排进队伍前, 先搜索队伍中是否有他的队友. 如果有, 这名成员就直接站在最后一个队友的后面,如果没有,那么这名成员只能排 ...

  9. 数据结构 happiness

    问题描述 这一天是小 V 的生日,他收到了朋友们送给他的礼物.现在,小 V 有 n 件礼物,他将这 n 件礼物排成一排,依次编号为 1 到 n,每件礼物都有一个满意值 w[i].现在小 V 要从中选取 ...

  10. 并发编程学习笔记之可见性&过期数据(二)

    想要使用多线程编程,有一个很重要的前提,那就是必须保证操纵的是线程安全的类. 那么如何构建线程安全的类呢? 1. 使用同步来避免多个线程在同一时间访问同一数据. 2. 正确的共享和安全的发布对象,使多 ...