力扣hot100 两数之和 哈希表

👨‍🏫 力扣 两数之和


😋 思路

复制代码
在一个数组中如何快速找到某一个数的互补数:哈希表 O(1)实现

⭐ AC code

java 复制代码
class Solution {
	public int[] twoSum(int[] nums, int target)
	{
		HashMap<Integer, Integer> map = new HashMap<>();
		for (int i = 0; i < nums.length; i++)
		{
			if (map.containsKey(target - nums[i]))
				return new int[] { map.get(target - nums[i]), i };
			map.put(nums[i], i);
		}
		return new int[0];
	}
}
相关推荐
AlenTech8 分钟前
647. 回文子串 - 力扣(LeetCode)
算法·leetcode·职场和发展
py有趣12 分钟前
力扣热门100题之合并两个有序链表
算法·leetcode·链表
8Qi827 分钟前
LeetCode热题100--45.跳跃游戏 II
java·算法·leetcode·贪心算法·编程
foundbug9991 小时前
基于STM32的步进电机加减速程序设计(梯形加减速算法)
stm32·单片机·算法
北顾笙9801 小时前
day12-数据结构力扣
数据结构·算法·leetcode
凌波粒1 小时前
LeetCode--454.四数相加 II(哈希表)
算法·leetcode·散列表
漫随流水2 小时前
c++编程:D进制的A+B(1022-PAT乙级)
数据结构·c++·算法
tankeven2 小时前
HJ159 没挡住洪水
c++·算法
美式请加冰2 小时前
斐波那契数列介绍和使用
算法
paeamecium2 小时前
【PAT】 - Course List for Student (25)
数据结构·c++·算法·pat考试