当前位置:网站首页>leetcode刷题:哈希表04 (两数之和)
leetcode刷题:哈希表04 (两数之和)
2022-06-23 17:23:00 【涛涛英语学不进去】
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
一上手我就是个暴力破解
public int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
//处理初始数组
for (int i = 0; i < nums.length; i++) {
//因为是两个数,temp为应该出现的结果
int temp = target - nums[i];
for (int j = 0; j < nums.length; j++) {
//不能一个数用两次
if (i == j) {
continue;
}
if (temp == nums[j]) {
result[0] = i;
result[1] = j;
return result;
}
}
}
return null;
}

显然。。不应该这么做。换个思路,用哈希表的思想。
public static int[] twoSum2(int[] nums, int target) {
int[] result = new int[2];
HashMap<Integer, Integer> map = new HashMap<>();
//处理初始数组
for (int i = 0; i < nums.length; i++) {
int temp = target - nums[i];
//根据键查找,需要的值是否存在
if (map.get(nums[i]) != null) {
for (int j = 0; j < nums.length; j++) {
if (temp == nums[j] && i != j) {
result[0] = i;
result[1] = j;
return result;
}
}
}
//把下一次需要的值作为键
map.put(temp, nums[i]);
}
return null;
}

边栏推荐
- 7、VLAN-Trunk
- Lighthouse open source application practice: o2oa
- ACM players take you to play with the array!
- 微信小程序报错[ app.json 文件内容错误] app.json: app.json 未找到
- SimpleDateFormat在多线程环境下存在线程安全问题。
- Mobile SSH connection tool
- The principle of MySQL index algorithm and the use of common indexes
- How to solve the problem that the esp8266-01s cannot connect to Huawei routers
- 《致敬百年巨匠 , 数藏袖珍书票》
- How to make a badge
猜你喜欢
![[esp8266-01s] get weather, city, Beijing time](/img/8f/89e6f0d482f482ed462f1ebd53616d.png)
[esp8266-01s] get weather, city, Beijing time

论文阅读 (50):A Novel Matrix Game with Payoffs of Maxitive Belief Structure

TT 语音落地 Zadig:开源共创 Helm 接入场景,环境治理搞得定!

如何利用好每天的时间高效复习?

What does the science and technology interactive sand table gain popularity by virtue of
![[failure announcement] there is a problem with the redis that replaces memcached, causing the website to fail](/img/b5/447faaee6d5d2d88927e84e17403ed.png)
[failure announcement] there is a problem with the redis that replaces memcached, causing the website to fail

VNC Viewer方式的远程连接树莓派
![[esp8266 - 01s] obtenir la météo, Ville, heure de Beijing](/img/8f/89e6f0d482f482ed462f1ebd53616d.png)
[esp8266 - 01s] obtenir la météo, Ville, heure de Beijing

The battlefield of live broadcast e-commerce is not in the live broadcast room

CSDN salary increase secret script: Jenkins integrated allure test report complete tutorial
随机推荐
论文阅读 (50):A Novel Matrix Game with Payoffs of Maxitive Belief Structure
客服系统搭建教程_宝塔面板下安装使用方式_可对接公众号_支持APP/h5多租户运营...
Crmeb second open SMS function tutorial
论文阅读 (58):Research and Implementation of Global Path Planning for Unmanned Surface Vehicle Based...
6、VLAN
论文阅读 (51):Integration of a Holonic Organizational Control Architecture and Multiobjective...
Redis 集群
2021 excellent TDP members' output data summary is coming, come and watch!!!
【win10 VS2019 opencv4.6 配置参考】
论文阅读 (52):Self-Training Multi-Sequence Learning with Transformer for Weakly Supervised Video Anomaly
实现领域驱动设计 - 使用ABP框架 - 存储库
Latex compiled successfully but could not be output to PDF
手机开户一般哪个证券公司好?在线开户安全么?
论文阅读 (54):DeepFool: A Simple and Accurate Method to Fool Deep Neural Networks
An error is reported when latex uses \usepackage{hyperref}: paragraph ended before [email protected] @link was complete
The battlefield of live broadcast e-commerce is not in the live broadcast room
Torch learning (I): environment configuration
Pause update Bulletin - walking Pikachu
3000帧动画图解MySQL为什么需要binlog、redo log和undo log
论文阅读 (55):Dynamic Multi-Robot Task Allocation under Uncertainty and Temporal Constraints