给定一个整数nums和一个目标值target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标。
可以假设每种输入只会对应一个答案。但是,不能重复利用这个数组中同样的元素。

题解

提交代码

class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++) {
if(map.containsKey(target - nums[i])) {
return new int[] {map.get(target - nums[i]), i};
}
map.put(nums[i],i);
}
throw new IllegalArgumentException("No two sum solution");
}
}

本题最简易方法:暴力法

  • 循环遍历两次,对于每个元素x遍历整个数组,判断是否存在target-x。

提交代码的思路:是用HashMap存储每个元素,每次存入元素的同时,判断是否存在target-x。

  • 两个方法不同之处是用空间换时间。
  • 熟悉了HashMap。

Leet Code 1.两数之和的更多相关文章

  1. Leet Code 2.两数相加

    2.两数相加 题目描述 给出两个非空的链表用来表示两个非负的整数.其中,它们各自的位数是按照逆序的方式存储的,并且它们的每个节点只能存储一位数字.如果,我们将这两个数相加起来,则会返回一个新的链表来表 ...

  2. LintCode-56.两数之和

    两数之和 给一个整数数组,找到两个数使得他们的和等于一个给定的数 target. 你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标.注意这里下标的范围是 1 到 n, ...

  3. 【LeetCode】 两数之和 twoSum

    两数之和 (简单) 题目描述 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数: 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 例如: 给定 nums = [2,7,11, ...

  4. 给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X

    题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的 ...

  5. LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  6. LeetCode 371. Sum of Two Integers (两数之和)

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  7. LeetCode 167. Two Sum II - Input array is sorted (两数之和之二 - 输入的是有序数组)

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  8. [LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

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

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

随机推荐

  1. mysql 安装与基本管理

    目录 mysql 安装与基本管理 MySQL介绍 下载安装 登录设置密码 破解密码 统一字符编码 mysql参数配置 mysql 常用指令 mysql 安装与基本管理 MySQL介绍 MySQL是一个 ...

  2. Centos6.10编译安装php-7.1.12并安装redis模块

    1.服务器初始化 yum update -yyum install epel-release -yyum install gcc gcc-c++ wget lsof lrzsz telnet -y 2 ...

  3. Spring Boot 配置文件中的花样

    原文:https://www.cnblogs.com/didispace/p/11002732.html 在快速入门一节中,我们轻松的实现了一个简单的RESTful API应用,体验了一下Spring ...

  4. keras模块学习之泛型模型学习笔记

    本笔记由博客园-圆柱模板 博主整理笔记发布,转载需注明,谢谢合作! Keras泛型模型接口是:  用户定义多输出模型.非循环有向模型或具有共享层的模型等复杂模型的途径  适用于实现:全连接网络和多输入 ...

  5. MyEclipse设置文件默认打开方式

    MyEclipse设置文件默认打开方式 依次点击: [MyEclipse]-> [Preferences] -> [General] -> [Editors] -> [File ...

  6. js数组的操作大全

    用 js有很久了,但都没有深究过js的数组形式.偶尔用用也就是简单的string.split(char).这段时间做的一个项目,用到数组的地方很多,自以为js高手的自己居然无从下手,一下狠心,我学!呵 ...

  7. DELL--R420 CPU报警“CPU0000 cpu2 internal error (IERR)contact support”

    按照以下操作解决: 请用户按以下操作,搞定. 请使用一台服务器测试: 开机看到dell标志时,按F2键→"System BIOS Setting" →"System Pr ...

  8. sizeof的注意点

    sizeof('a')的值为4.因为此处‘a’是独立存在的一个字符(没有赋值给其它变量),实际上就是一个整型数,占4个字节,即此处‘a’对应的ascii码的十进制为整数97.(貌似解释得有些牵强,但事 ...

  9. 接口-DBLINK初尝试

    需求: 将寿险核心库中的黑名单数据提取到团险核心中,供团险核心使用,并且在核心前端页面需配置对应的菜单,提供相应的按钮,该接口采用dblink的方式进行提取. 通过本地数据库配置dblink访问远程数 ...

  10. netty: 以默认的ByteBuf作为传输数据

    client部分代码: //线程 EventLoopGroup worker = new NioEventLoopGroup(); //辅助类 Bootstrap b = new Bootstrap( ...