给定一个整数数组 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. 量化研究之“大A打板敢死队”是如何做换手板与撬板的?

    更多精彩内容,欢迎关注公众号:数量技术宅,也可添加技术宅个人微信号:sljsz01,与我交流. 涨停跌停板分类 涨停.跌停是A股特有的现象,其他主要市场,例如美股.港股都不存在涨跌停的规则.涨停.跌停 ...

  2. Qt:QNetworkReply

    0.说明 QNetworkReply对象包含了Manager发送的请求头和返回的数据. 它继承自QIODevice,所以可以用各种read获取其中返回的数据: QByteArray data = re ...

  3. Python:List

    1.List相关的操作符 操作符 说明 例子 * 重复,将List重复若干遍放到同一个List中 ['hi'] * 3 ['hi' , 'hi' , 'hi'] + 合并两个List(作用和appen ...

  4. 浅谈cache

    2021.9.22更新: <浅谈Cache Memory> http://blog.sina.com.cn/s/blog_6472c4cc0102dusv.html 为什么贴上这个链接呢, ...

  5. 使用MASA Blazor开发一个标准的查询表格页

    前言 大家好,我是开源项目 MASA Blazor 主要开发者之一,如果你还不了解MASA Blazor,可以访问我们的 官网 和博客 <初识MASA Blazor> 一探究竟.简单来说, ...

  6. http1.1与http2.0

    简介 http1.0: 1.0版本中每个TCP连接只能发送一个请求,数据发送完毕连接就关闭,如果还要请求其他资源,就必须重新建立TCP连接.(TCP为了保证正确性和可靠性需要客户端和服务器三次握手和四 ...

  7. CentOS 8: yum仓库配置

    在CentOS 8中,使用yum时出现错误,镜像列表中没有url,类似如下: Error: Failed to download metadata for repo 'appstream': Cann ...

  8. eval()计算某个字符串,js和jquery都可以使用

    实例 执行JavaScript代码或表达式: <script>eval("x=10;y=20;document.write(x*y)");document.write( ...

  9. 软件工程homework-003

    软件工程第三次作业 博客信息 沈阳航空航天大学计算机学院2020软件工程作业 作业要求 软件工程第三次作业 课程目标 熟悉一个"高质量"软件的开发过程 作业目标 熟悉代码规范及结对 ...

  10. R-CNN小结

    1.背景 物体检测(object detection)是计算机视觉非常重要的一个领域.RCNN作为该领域的开山鼻祖,在深度学习出现之前,传统方法始终无法处理好物体检测问题(会通过非常庞大的计算,来算出 ...