给定一个整数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. 解决spring boot1.5以上版本@ConfigurationProperties提示“Spring Boot Configuration Annotation Processor not.."

    Springboot1.5以上版本,在使用 @ConfigurationProperties注解的时候会提示“Spring Boot Configuration Annotation Processo ...

  2. Celebrate it, this is my first time on this blog.

    After hovered around the technology edge many years(so ashamed), I want to collected all the points ...

  3. 【转】win10硬盘序列号查看方法

    原文:https://zixue.3d66.com/changjianwenti/tiwen_9679.html ------------------------------------------- ...

  4. 【Java】《Java程序设计基础教程》第四章学习

    4.1 类的封装 封装指的是将东西包装在一起,然后以新的完整形式呈现.包含两个意义: 1). 把对象的全部属性和方法结合在一起,形成一个不可分割的独立单位(即对象). 2). 信息隐藏,即尽可能隐藏对 ...

  5. maven中profile的使用

    转载:https://blog.csdn.net/java_collect/article/details/83870215 前言       在开发过程中,我们的项目会存在不同的运行环境,比如开发环 ...

  6. 二.protobuf3数据类型

    定义数据类型 首先让我们看一个非常简单的例子.假设您想要定义搜索请求消息格式,其中每个搜索请求都有一个查询字符串.您感兴趣的特定结果页面以及每页的结果数量.这是用来定义消息类型的.proto文件. s ...

  7. PHP导出数据库sql文件,add和update

    /*** 导出sql文件*/public function exportSql(){   //需要导出的数据库表存入到数组当中   $tables =array("T_CRM_QUEUE_F ...

  8. 接口-DBLINK初尝试

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

  9. xml文件整理

    xml 97-2003 格式 \s*\n\s*\n\s*\n\s*\n\n(^个人补充信息.*)\n(.*)\n(^总成绩.*)$1$2\n$3(^个人补充信息.*)\n(.*)\n(.*)\n(^总 ...

  10. bzoj 2969: 矩形粉刷 概率期望+快速幂

    还是老套路:期望图上的格子数=$\sum$ 每个格子被涂上的期望=$\sum$1-格子不被图上的概率 这样的话就相对好算了. 那么,对于 $(i,j)$ 来说,讨论一下上,下,左,右即可. 然后发现四 ...