leetcode_两数之和
给定一个整数数组 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_两数之和的更多相关文章
- LeetCode_#1_两数之和 Two Sum_C++题解
1. 两数之和 Two Sum 题目描述 Given an array of integers, return indices of the two numbers such that they ad ...
- 给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X
题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的 ...
- LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- 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 ...
- 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 ...
- [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 ...
- [LeetCode] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
- Leetcode(一)两数之和
1.两数之和 题目要求: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重 ...
- 南大算法设计与分析课程OJ答案代码(1)中位数附近2k+1个数、任意两数之和是否等于给定数
问题1 用来测试的,就不说了 问题2:中位数附近2k+1个数 给出一串整型数 a1,a2,...,an 以及一个较小的常数 k,找出这串数的中位数 m 和最接近 m 的小于等于 m 的 k 个数,以及 ...
随机推荐
- idea maven问题汇总
目录 idea问题.maven问题汇总 解决方法汇总 idea问题.maven问题汇总 idea maven依赖包报can't resolve问题 代码飘红 解决办法:删除所有.idea等idea相关 ...
- Hadoop权威指南 - 学习笔记
初识Hadoop.关于MapReduce Hadoop宏观介绍 相对于其他系统的优势 关系型数据库管理系统 为什么不能用配有大量硬盘的数据库进行大规模分析?为什么需要Hadoop? 因为计算机硬盘的发 ...
- 小白学python第2问: 为什么只有int,没有long?
为什么只有int,没有long? 在python官网开发者指引里面能找到 PEP 237 -- Unifying Long Integers and Integers,这里说明了为什么要统一 int ...
- Windows Server 2012 R2通过命令行重置网络环境
转至:https://jingyan.baidu.com/article/48b37f8d5d89385a646488b5.html 我们使用Windows Server 2012 R2时会遇到通过命 ...
- PIL-ImageFont:OSError: cannot open resource
使用PIL时,创建某个字体Font对象时出错: font=ImageFont.truetype('Arial.ttf',36) 可能原因有两个: 1.PIL无法定位到字体文件的位置. 可以通过提供绝对 ...
- 面向对象编程(C++篇2)——构造
目录 1. 引述 2. 详述 2.1. 数据类型初始化 2.2. 类初始化 1. 引述 在C++中,学习类的第一课往往就是构造函数.根据构造函数的定义,构造函数式是用于初始化类对象的数据成员的.无论何 ...
- java中如何将嵌套循环性能提高500倍
java中如何将嵌套循环性能提高500倍 转载请注明出处https://www.cnblogs.com/funnyzpc/p/15975882.html 前面 似乎上一次更新在遥远的九月份,按照既定的 ...
- VirtualBox 桥接模式
网桥网络配置 以下内容来自:http://www.jianshu.com/p/a4dbdb40b72b 特点 1.如果主机可以上网,虚拟机可以上网 2.虚拟机之间可以ping通 3.虚拟机可以ping ...
- 2022年官网下安装RabbitMQ最全版与官网查阅方法
目录 一.Erlang环境部署 1.百度搜索"Erlang",或者访问网址:https://www.erlang.org/,找到DOWNLOAD双击进入. 2.找到支持的windo ...
- elasticsearch高亮之词项向量
一.什么是词项向量 词项向量(term vector)是有elasticsearch在index document的时候产生,其包含对document解析过程中产生的分词的一些信息,例如分词在字段值中 ...