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 个数,以及 ...
随机推荐
- URI/URL/URN都是什么
URI : Uniform Resource Identifier,统一资源标志符 URL : Universal Resource Locator,统一资源定位符 URN : Universal R ...
- 【实测】Python 和 C++ 下字符串查找的速度对比
完整格式链接:https://blog.imakiseki.cf/2022/03/07/techdev/python-cpp-string-find-perf-test/ 背景 最近在备战一场算法竞赛 ...
- 从零开始Pytorch-YOLOv3【笔记】(一)配置文件解读
前言 这是github上的一个项目YOLO_v3_tutorial_from_scratch,它还有相应的blog对其详细的解读.机器之心翻译了他的tutorial:从零开始PyTorch项目:YOL ...
- 如何写Markdown格式文档
Markdown Markdown是一种轻量级标记语言,创始人为约翰·格鲁伯.它允许人们使用易读易写的纯文本格式编写文档,然后转换成有效的XHTML(或者HTML)文档.这种语言吸收了很多在电子邮件中 ...
- Laravel 8 图片上传七牛云
1.利用 composer 下载依赖包 composer require itbdw/laravel-storage-qiniu 2.打开 config 文件夹下的 app.php 文件,在 prov ...
- 浅识 npm 与 cnpm
npm是什么? Node Package(包) Manager(管理器) 的简称. 在 GitHub 还没有兴起的年代,前端是通过网址来共享代码.比如你想使用 jQuery,那么你点击 jQuery ...
- JDK ThreadLocal解析
Java ThreadLocal解析 ThreadLocal 线程本地变量, 线程私有, 在 Thread 类中用 ThreadLocal.ThreadLocalMap threadLocals 以数 ...
- 宇宙最強的IDE - Visual Studio 25岁生日快乐
每位开发者从入门开始或多或少都会接触过 Visual Studio , 现今的 Visual Studio 除了支持传统的 C++ , C# , Visual Basic.NET ,F# 的编程语言外 ...
- kkFileView对接svn服务完成文件在线预览功能
1.需求: 之前在公司内部搭建了svn服务器,给部门存放文档.视频,做成了一个文档服务器来用,随着视频文件太大,每次下载太慢 需要把文件在线打开查看 2.解决: kkFileView https:// ...
- AT1219题解
题意 设 \(a\) 的价值为 \(a \times cnt_a\)(\(cnt_a\) 为 \(a\) 在区间中出现的次数),求区间种某种元素,使得这种元素的价值最大. 因为设计出现元素的次数,所以 ...