package 两数之和;
import java.util.HashMap;
//给定一个整数数组nums和一个整数目标值 target,
//请你在该数组中找出和为目标值target的那两个整数,并返回它们的数组下标。
//你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
//你可以按任意顺序返回答案。
public class 哈希表 {
public static void main(String[] args) {
int[] nums = new int[]{2, 6, 11, 15};
int target = 8;
int[] result = twoSum(nums, target);
for (int i = 0; i < result.length; i++) {
if (i == 0) {
System.out.print("[" + result[i] + ",");
i++;
}
if (i == result.length - 1) {
System.out.print(result[i] + "]");
break;
}
System.out.print(result[i] + ",");
}
}
private static int[] twoSum(int[] nums, int target) {
//hashmap数据结构底层链表加数组,存储形式是Key-value
//(数组是主体,链表为了解决哈希碰撞问题-两个对象调用hashcode方法计算出来的哈希值一致)
//创建一个hashmap对象,用于存储整数类型的键值对
HashMap<Integer, Integer> hashtable = new HashMap<>();
//遍历数组中的每一个元素
for (int i = 0; i < nums.length; i++) {
if (hashtable.containsKey(target - nums[i])) {
//hashmap对象里存在匹配所需要的k,根据k获取数据在数组里对应的索引值
return new int[]{hashtable.get(target - nums[i]), i};
}
//K存储值,V存储索引
hashtable.put(nums[i], i);
}
//没有匹配到,返回一个空数组
return new int[0];
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
java语言练习算法.zip
资源推荐
资源详情
资源评论
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 6 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/72a9936e28d84a44b8d02dcbe3729b26_m0_62143653.jpg!1)
Matlab仿真实验室
- 粉丝: 3w+
- 资源: 2192
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)