当前位置:网站首页>每日一题:两数之和
每日一题:两数之和
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;
}
}边栏推荐
- 数组和指针(2)
- PHP message feedback management system source code
- How does the new retail saas applet explore the way to break the digital store?
- CMake库搜索函数居然不搜索LD_LIBRARY_PATH
- 23. 请你谈谈关于IO同步、异步、阻塞、非阻塞的区别
- 第一次用debug查询,发现这个为空,是不是代表还没获得数据库的意思?求帮助。
- The case of five little pigs (five little pigs compare the size of the body weight)
- [TypeScript] Introduction, Development Environment Construction, Basic Types
- Leetcode 118. 杨辉三角
- 第六章:决胜秋招
猜你喜欢
随机推荐
The way of life, share with you!
Goland opens file saving and automatically formats
UI测试新方法:视觉感知测试详解
字符串加千分位符与递归数组求和
huato hot update environment construction (DLL method hot update C# code)
[NCTF2019] Fake XML cookbook-1|XXE vulnerability|XXE information introduction
对话框 QDialog ( 详解 )
(1) Cloud computing technology learning - virtualized vSphere learning
Wuhan Star Sets Sail: Overseas warehouse infrastructure has become a major tool for cross-border e-commerce companies to go overseas
Public Key Retrieval is not allowed报错解决方案
初识二叉搜索树
[NCTF2019]Fake XML cookbook-1|XXE漏洞|XXE信息介绍
DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计
[flutter] What is MaterialApp and Material design
Windows MySQL 安装配置
How to intercept the first few digits of a string in php
Mirror stand to collect
数据的存储
Public Key Retrieval is not allowed error solution
Qt 动态库与静态库









