给定一个整数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. 【cookie的使用】&【Session】

    明确一点:        cookie由服务器创建Cookie cookie=new Cookie("haha","xixi") 通过HtttpServletR ...

  2. laravel使用withCount获取列表下关联模型的数量

    模型里面 <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Post extends Model ...

  3. ClickHouse学习笔记

    1. 概述 ClickHouse是一个用于联机分析(OLAP:Online Analytical Processing)的列式数据库管理系统(DBMS:Database Management Syst ...

  4. docker入门一:docker安装(在线跟离线)

    一.在线安装 1.安装依赖 yum install -y yum-utils device-mapper-persistent-data lvm2 2.添加软件源 yum-config-manager ...

  5. homebrew一直处于updating状态

    vim ~/.bash_profile 增加一行 export HOMEBREW_NO_AUTO_UPDATE=true 之后再source一下

  6. pyspark读取hdfs 二进制文件

    程序如下: from pyspark import SparkConf, SparkContext conf = SparkConf().setAppName("My test App&qu ...

  7. Python工程目录组织

    Python工程目录组织 from: https://zhuanlan.zhihu.com/p/36221226 Python工程目录组织 关于如何组织一个较好的Python工程目录结构,已经有一些得 ...

  8. 一 创建一个springboot项目之(微信点餐系统的设计与开发)

    第一步:收到项目需求,进行数据库表的设计. 1.角色的划分: 卖家:  订单,类目 买家:  商品列表 2.功能模块的划分: 商品:商品列表 订单:  订单创建,订单查询,订单取消 类目:基于管理的功 ...

  9. 用 Python 加密文件

    生活中,有时候我们需要对一些重要的文件进行加密,Python 提供了诸如 hashlib,base64 等便于使用的加密库. 但对于日常学习而言,我们可以借助异或操作,实现一个简单的文件加密程序,从而 ...

  10. 2019-2020-1 20199312《Linux内核原理与分析》第十二周作业

    实验背景 2014年9月24日,Bash中发现了一个严重漏洞shellshock,该漏洞可用于许多系统,并且既可以远程也可以在本地触发.在本实验中,学生需要亲手重现攻击来理解该漏洞,并回答一些问题. ...