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;}
}

转载于:https://www.cnblogs.com/yjxyy/p/10680515.html

1.Tow Sum(两数和)相关推荐

  1. leetcode之Tow Sum两数之和的三种思路

    双重循环.桶排序.HashMap 题目链接:两数之和 1.双重循环,最基本的方法,速度慢O(n^2),但无需新空间. public int[] twoSum(int[] nums, int targe ...

  2. Two Sum (两数之和) - Hash Table (哈希表)

    Two Sum (两数之和) - Hash Table (哈希表) https://leetcode-cn.com/problems/two-sum/ Given an array of intege ...

  3. LeetCode in Python-1. Two Sum 两数之和

    Two Sum 两数之和 题目描述 解法1.切片后查找 解法2.hash字典 解法3.同2 出处 题目描述 解法1.切片后查找 class Solution:def twoSum(self, nums ...

  4. [Leetcode]Two sum(两数之和)系列总结

    Two sum 题目 Given an array of integers, return indices of the two numbers such that they add up to a ...

  5. 0001-Two Sum(两数之和)

    这个系列算是出于个人兴趣开的一个新坑吧,最近看到同学刷LeetCode算法题,就想写写那些可以一行Python代码写出来的题目,因此本专栏的文章的解题方式效率不做保证,只为追求"一行的浪漫& ...

  6. LeetCode刷题-两数之和(持续更新)

    文章目录 LeetCode 1. Two Sum (两数之和) 题目描述 样例 解题思路一(暴力法) 解题思路二(使用map) 前言:最近业余时间,一直在看LeetCode上面的题,上面有许多好的解题 ...

  7. 领扣-1/167 两数之和 Two Sum MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  8. leetcode算法—两数之和 Two Sum

    关注微信公众号:CodingTechWork,一起学习进步. 题目 Two Sum: Given an array of integers, return indices of the two num ...

  9. 167. Two Sum II - Input array is sorted两数之和

    1. 原始题目 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明 ...

  10. 两数之和(Two Sum)

    文章目录 题目 一.暴力算法 二.利用hashMap键的唯一性 题目 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回 ...

最新文章

  1. window.name实现的跨域数据传输
  2. Sublime Text 3便携版下载安装和常用插件安装--顺便解决报错An error occured installing和no packages available for install
  3. SQLServer之创建INSTEAD OF INSERT,UPDATE,DELETE触发器
  4. WARN ServletController:171 - Can't find the the request for xxxx's Observer
  5. verilog always语法_Verilog初级教程(20)Verilog中的`ifdef 条件编译语句
  6. boost::safe_numerics模块实现混合类型产生令人惊讶的结果的测试程序
  7. DockOne微信分享(八十四):Docker在B站的实施之路
  8. python psutil库安装_安装psutil模块报错安装python-devel
  9. 设计模式学习笔记-观察者模式
  10. 句法分析(syntactic parsing)在NLP领域的应用是怎样的
  11. 基于 bootstrap 字体图标,用纯CSS实现星级评分功能
  12. win64位下安装python的image支持
  13. Linux(CentOS7.1)修改默认yum源为国内的阿里云yum源
  14. 【转】收集各种反编译工具 常用EXE文件反编译工具下载
  15. Python -- 网络编程 -- 抓取网页图片 -- 豆瓣妹子
  16. matlab中如何取正数,excel表格数据取正值-怎么把excel表格的负数变成正数
  17. c语言中的output用法,OUTPUT 子句
  18. matlab解韩信点兵问题,2019事业单位:巧解“韩信点兵”问题
  19. 基于深度卷积神经网络的图像超分辨率重建(SRCNN) 学习笔记
  20. 构建具有丰富功能的交互式 Kiosk

热门文章

  1. php封装app微信支付
  2. vue基础知识点思维导图
  3. BigInteger简单使用及方法总结
  4. Maven pom.xml 文件格式详解一
  5. @Slf4j注解的使用
  6. PuTTY免输密码自动登录Linux,让 Putty 保存密码,自动登陆的三种方法
  7. 阶段3 2.Spring_07.银行转账案例_8 基于接口的动态代理回顾
  8. Web.xml in Hello1 project
  9. CF125E MST Company
  10. TCP客户端与服务器的实现