当前位置:网站首页>每日一题:两数之和
每日一题:两数之和
2022-07-30 16:41:00 【#小苏打】

思路:
利用哈希表,当判断是否重复,是否有,这种问题一般采用哈希表结构,该题采用map,key为数值,value为下标,(因为我们需要利用key的数值寻找数组的下标), 实则一个tep值,为 target - num[i] 的值,然后到map中需要是否存在符合该值的key,如果有,返回value
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer,Integer> map = new HashMap();
int[] res = new int[2];
if(nums == null||nums.length == 0){
return res;
}
for(int i = 0;i<nums.length;i++){
int temp = target - nums[i];
if(map.containsKey(temp)){
res[0] = map.get(temp);
res[1] = i;
}
map.put(nums[i],i);
}
return res;
}
}边栏推荐
- 【SOC FPGA】外设KEY点LED
- 如何在 UE4 中用代码去控制角色移动
- 【Linux Operating System】 Virtual File System | File Cache
- Scheduling_Channel_Access_Based_on_Target_Wake_Time_Mechanism_in_802.11ax_WLANs
- gvim命令记录
- The service already exists!解决办法
- Rounding out the most practical way of several DLL injection
- PCIE入门
- 疫情之下的裁员浪潮,7点建议帮你斩获心仪offer
- Public Key Retrieval is not allowed报错解决方案
猜你喜欢

完美绕开CRC32检测的无痕hook

Large-scale integrated office management system source code (OA+HR+CRM) source code sharing for free

大型综合办公管理系统源码(OA+HR+CRM)源码免费分享

如何写一份高可读性的软件工程设计文档

What does a good resume look like in the eyes of a big factory interviewer?

MySQL详细学习教程(建议收藏)

Visual Studio编辑器 2019:scanf函数返回值被忽略(C4996)报错及解决办法

Qt 容器控件之Tab Widget 使用详解

疫情之下的裁员浪潮,7点建议帮你斩获心仪offer

rhce笔记2
随机推荐
新人学习小熊派网络应用开发
rhce笔记2
The service already exists!解决办法
lotus 1.16.0 最小快照导出 导入
You are a first-class loser, you become a first-class winner
测试管理与规范
[NCTF2019] Fake XML cookbook-1|XXE vulnerability|XXE information introduction
镜像站收集
23. Please talk about the difference between IO synchronization, asynchronous, blocking and non-blocking
No qualifying bean of type问题解决
(1) Cloud computing technology learning - virtualized vSphere learning
arcpy tutorial
为什么中年男人爱出轨?
如何快速拷贝整个网站所有网页
详解最实用的几种dll注入方式
Lotus 1.16.0 minimum snapshot export import
Public Key Retrieval is not allowed报错解决方案
基于STM32F407使用ADC采集电压实验
The case of five little pigs (five little pigs compare the size of the body weight)
游戏窗口化的逆向分析