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. mongodb用mongoose得到的对象不能增加属性解决

    一,先定义了一个goods(商品)的models var mongoose = require('mongoose'); var Schema = mongoose.Schema; var produ ...

  2. 【持久化框架】SpringMVC+Spring4+Mybatis3集成,开发简单Web项目+源码下载

    上篇博文我们介绍了mybatis的基本概念与原理,这篇博文我们通过Spring与Mybatis集成,开发一个简单用户增删改查的Web项目. 基本准备工作 1.安装JDK1.6以上版本,安装与配置 2. ...

  3. Android指针管理:RefBase,SP,WP (二)

    (1)在Android中,RefBase结合了sp和wp,实现了一套通过引用计数的方法来控制对象声明周期的方法. RefBase的定义在/frameworks/base/include/utils/R ...

  4. import time

    时间相关的操作,时间有三种表示方式: 时间戳               1970年1月1日之后的秒,即:time.time() 格式化的字符串    2014-11-11 11:11,    即:t ...

  5. 为Docker镜像添加SSH服务

    一.基于commit命令创建 1. 首先下载镜像 $ docker run -it ubuntu:16.04 /bin/bash 2. 安装SSH服务 #更新apt缓存 root@5ef1d31632 ...

  6. Oracle pl/sql 记录、表类型

    一.PL/SQL记录 定义: TYPE <类型名> IS RECORD <列名1 类型1,列名2 类型2,...列名n 类型n,> [NOT NULL] <列的类型> ...

  7. Android禁止程序自动旋转的配置

    在想要禁止的Activity中加入 android:screenOrientation="portrait" 属性,其中,portrait是竖屏,landscape是横屏

  8. nginx关闭php报错页面显示

    默认情况下nginx是会显示php的报错的,如果要关闭报错显示,需要在/usr/local/php7/etc/php-fpm.d/www.conf文件里面设置,貌似默认情况下在php.ini关闭没效果 ...

  9. tarjan进阶

    一.边双连通分量 定义 若一个无向图中的去掉任意一条边都不会改变此图的连通性,即不存在桥,则称作边双连通图.一个无向图中的每一个极大边双连通子图称作此无向图的边双连通分量. 实际求法和强连通分量差不多 ...

  10. centos安装中文字体

    1.查看字体列表 2.将需要上传的字体上传至linux服务器/usr/share/fonts/chinese目录下 3.修改chinese目录的权限 chmod -R /usr/share/fonts ...