给定一个整数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. CentOS7.5 部署Ceph luminous

    环境 两台CentOS7.5,每台各两块硬盘部署OSD public network = 10.0.0.0/24 cluster network = 172.16.0.0/24 导入ceph的rpm ...

  2. 安装sqlite3

    说明 当前操作在root用户下执行 1.安装编译工具 yum -y groupinstall "Development tools" yum -y install zlib-dev ...

  3. c# FileStream 类构造函数

  4. log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment)的解决

    报错:log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironme ...

  5. 使用Cloudera Manager搭建MapReduce集群及MapReduce HA

    使用Cloudera Manager搭建MapReduce集群及MapReduce HA 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   一.通过CM部署MapReduce On ...

  6. CDH构建大数据平台-使用自建的镜像地址安装Cloudera Manager

    CDH构建大数据平台-使用自建的镜像地址安装Cloudera Manager 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   一.搭建CM私有仓库 详情请参考我的笔记: http ...

  7. jqGrid行编辑配置,方法,事件

    行编辑可以在行修改后更新数据,如下图所示 用户用鼠标点击选择一行,jqGrid将可编辑的字段转换为数据输入单元,如上面图所示.不可编辑的列,如id,不会转为可输入单元,而是保持不变.可以通过配置col ...

  8. 使用HashMap,如果key是自定义的类,就必须重写hashcode()和equals()

    java编程里有关约定:如果两个对象根据equals方法比较是相等的,那么调用这两个对象的任意一个hashcode方法都必须产生相同的结果. hashcode()和equals()都继承于object ...

  9. vue input只允许输入数字

    template: <input type="text" v-model="pageIndex" @keyup="inputChange&quo ...

  10. HTTP的幂等性

    转自: https://www.jianshu.com/p/234cf2e96832 理解HTTP幂等性基于HTTP协议的Web API是时下最为流行的一种分布式服务提供方式.无论是在大型互联网应用还 ...