给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标。

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。

你可以按任意顺序返回答案。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/two-sum

方法一:暴力枚举

遍历数组中的的每一个x,寻找是否存在target-x。

由于每一个位于x之前的元素都与x匹配过,因此每次遍历只需从x之后的元素开始寻找target-x。

代码:

public class Test_1_1 {
public static void main(String[] args) {
int nums[]=new int[]{4,7,1,9,6};
int target=8;
int twoSum[]= Test_1_1.twoSum(nums,target);
//测试
for (int i:twoSum){
System.out.println(i);
}
}
public static int[] twoSum(int[] nums, int target) {
//定义返回的索引数组
int twoindex[]=new int[2];
//遍历前length-1个元素,寻找后面是否有与之匹配的元素
for (int i=0;i<nums.length-1;i++){
//只需从nums[i]之后寻找
for (int j=i+1;j<nums.length;j++){
//若匹配到则存储索引下标
if (nums[i]+nums[j]==target){
twoindex[0]=i;
twoindex[1]=j;
}
}
}
return twoindex;
}
}

运行结果:

方法二:哈希表查找

遍历数组中每一个x元素,先在哈希表中查找是否存在key值为target-x,如果没有,则将x作为key值,索引作为value存入哈希表中,方便后续查找;如果有,则取出相应的value值。

代码:

public class Test_1_2 {
public static void main(String[] args) {
int nums[]=new int[]{8,0,9,1,8,7};
int target=10;
int a[]=Test_1_2.twoSum(nums,target);
//测试
for (int i:a){
System.out.println(i);
} }
public static int[] twoSum(int[] nums, int target) {
int num[]=new int[2];
//定义哈希表
Map<Integer,Integer> map=new HashMap<>();
//遍历每一个x元素
for (int i=0;i<nums.length;i++){
int j=target-nums[i];
//若在map中找到与x匹配的值,则取出索引
if (map.containsKey(j)){
num[1]=i;
num[0]=map.get(j);
}
//没有则将x作为key值存入map中
else {
map.put(nums[i],i );
}
}
return num;
}
}

运行结果:

leetcode_两数之和的更多相关文章

  1. LeetCode_#1_两数之和 Two Sum_C++题解

    1. 两数之和 Two Sum 题目描述 Given an array of integers, return indices of the two numbers such that they ad ...

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

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

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

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

  4. 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 ...

  5. 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 ...

  6. [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 ...

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

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

  8. Leetcode(一)两数之和

    1.两数之和 题目要求: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重 ...

  9. 南大算法设计与分析课程OJ答案代码(1)中位数附近2k+1个数、任意两数之和是否等于给定数

    问题1 用来测试的,就不说了 问题2:中位数附近2k+1个数 给出一串整型数 a1,a2,...,an 以及一个较小的常数 k,找出这串数的中位数 m 和最接近 m 的小于等于 m 的 k 个数,以及 ...

随机推荐

  1. IDE 、SDK 、API区别、库、框架、组件、CLI

    IDE:集成开发环境:包括代码编辑器.代码检测.代码调试器.译器/解释器.以及其他工具 SDK:SDK是IDE的基础引擎 ,比IDE更基本,因为它通常没有图形工具.工程师为辅助开发某类软件的相关文档. ...

  2. c语言结构体中的一个char数组怎么赋值?

    目录 前景提示 这里的结构体处理的步骤 一.char数组类型的处理 二.char数组指针类型的处理 三.全部代码 1. char数组 2. char数组指针 结语 前景提示 定义一个结构体,结构体中有 ...

  3. 使用Oracle的PROFILE对用户资源限制和密码限制

    转至:https://blog.csdn.net/zhuomingwang/article/details/6444240?utm_medium=distribute.pc_relevant.none ...

  4. Python:pandas(二)——pandas函数

    Python:pandas(一) 这一章翻译总结自:pandas官方文档--General functions 空值:pd.NaT.np.nan //判断是否为空 if a is np.nan: .. ...

  5. Python:Excel

    xlrd与xlwt:xls文件 如果不想看前半部分的基础知识,可以直接看最后的总结部分 1.两个模块 读xlrd 写xlwt import xlrd,xlwt 2.读 2.1 文件.表格信息的获取 打 ...

  6. Django的orm(一)

    Django的orm一 1.创建表 1.1 创建普通表 class UserType(models.Model): ''' 用户类型 ''' title=models.CharField(max_le ...

  7. JDK下载安装与环境变量配置【全网最新】

    1.下载安装JDK 下载地址:(https://www.oracle.com/java/technologies/downloads/) 最好选择解压版,解压即可(说删就删) 解压:例如我解压目录为 ...

  8. JZ-015-反转链表

    反转链表 题目描述 输入一个链表,反转链表后,输出新链表的表头. 题目链接: 反转链表 代码 /** * 标题:反转链表 * 题目描述 * 输入一个链表,反转链表后,输出新链表的表头. * 题目链接: ...

  9. 基于Kubernetes/K8S构建Jenkins持续集成平台(下)

    基于Kubernetes/K8S构建Jenkins持续集成平台(下) Jenkins-Master-Slave架构图回顾: 安装和配置NFS NFS简介 NFS(Network File System ...

  10. U3D破解方法

    文件我的云盘里面有,然后操作如下:Win版破解方法:1.安装Unity后(联网破解不成功就断网试试),运行破解补丁.2.点击Browse选择Unity所在路径 例如我的Unity安装路径 F:\Sof ...