当前位置:网站首页>每日一题:两数之和
每日一题:两数之和
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;
}
}边栏推荐
- The service already exists!解决办法
- 牛客网刷题——运算符问题
- arcpy tutorial
- PCIE入门
- Paper reading (63): Get To The Point: Summarization with Pointer-Generator Networks
- win下搭建php环境的方法
- 华为云数据治理生产线DataArts,让“数据'慧'说话”
- Lotus 1.16.0 minimum snapshot export import
- 探究CSAPP实验二-bomb lab-第一节
- How to intercept the first few digits of a string in php
猜你喜欢

Leetcode 119. 杨辉三角 II

Public Key Retrieval is not allowed报错解决方案

论文阅读 (63):Get To The Point: Summarization with Pointer-Generator Networks

登录模块调试-软件调试入门

The first time I used debug query and found that this was empty, does it mean that the database has not been obtained yet?please help.

【Linux操作系统】 虚拟文件系统 | 文件缓存

Login Module Debugging - Getting Started with Software Debugging

探究CSAPP实验二-bomb lab-第一节

PCIE入门

Visual Studio 集成Qt开发环境的一些注意事项
随机推荐
安全业务收入增速超70% 三六零筑牢数字安全龙头
在 Chrome 浏览器中安装 JSON 显示插件
Visual Studio编辑器 2019:scanf函数返回值被忽略(C4996)报错及解决办法
hcip--ospf综合实验
HUAWEI CLOUD data governance production line DataArts, let "data 'wisdom' speak"
swagger使用教程——快速使用swagger
游戏多开检测的几种实现方法及破解方法参考
Qt 动态库与静态库
vivo announced to extend the product warranty period, the system launched a variety of functional services
How to intercept the first few digits of a string in php
游戏窗口化的逆向分析
SMI 与 Gateway API 的 GAMMA 倡议意味着什么?
node.js中怎么连接redis?
加密生活,Web3 项目合伙人的一天
Wuhan Star Sets Sail: Overseas warehouse infrastructure has become a major tool for cross-border e-commerce companies to go overseas
如何快速拷贝整个网站所有网页
基于STM32F407使用ADC采集电压实验
vivo宣布延长产品保修期限 系统上线多种功能服务
How to connect redis in node.js?
SocialFi 何以成就 Web3 去中心化社交未来