当前位置:网站首页>【Hot100】1. Sum of two numbers
【Hot100】1. Sum of two numbers
2022-06-28 16:18:00 【Wang Liuliu's it daily】
Simple questions
Given an array of integers nums And an integer target value target, Please find... In the array And is the target value the Two Integers , And return their array subscripts .
solution :
We need a method that can quickly find whether the target element exists in the array . If there is , Need to find its index .
Use hash table , You can look for target - nums[i] The time complexity is reduced to from O(N) Down to O(1).
Create a Hashtable , For each of these nums[i], First, query the hash table to see if target - nums[i], And then nums[i] Insert into hash table , You can guarantee that you won't let nums[i] Match yourself .
Points of attention :
①map:
key-> Array value
val-> Subscript
② Already exist map The subscript in must be in front , therefore j When the result is returned to the array, put map Put the subscript in the front .
③ return null Two ways of writing :
return null;
return new int[0];
Code :
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> map = new HashMap<>();
for(int i=0;i<nums.length;i++){
int temp = target - nums[i];
if(map.containsKey(temp)){
return new int[]{
map.get(temp),i};
}
map.put(nums[i],i);
}
return new int[0];
}
}

边栏推荐
- 面试官: 线程池是如何做到线程复用的?有了解过吗,说说看
- Kiss in the metauniverse! CMU launched VR head display plug-in, reproducing the vivid touch of lips
- 机器学习之卷积神经网络使用cifar10数据集和alexnet网络模型训练分类模型,安装labelimg,以及报错ERROR
- A new 25K byte from the Department showed me what the ceiling is
- among us私服搭建
- tablestore中可以使用sql查询可以查出表中所有的数据吗?
- The first place on the list - brake by wire "new cycle", the market competitiveness of local suppliers is TOP10
- QQ appears large-scale number theft, why is this? Is there no solution?
- 10:00面试,10:02就出来了 ,问的实在是太...
- Mysql自连接查询「建议收藏」
猜你喜欢
随机推荐
机器学习之卷积神经网络Lenet5训练模型
简单介绍一下tensorflow与pytorch的相互转换(主要是tensorflow转pytorch)
Redmibook Pro 14 enhanced version cannot open delta software drastudio_ v1.00.07.52
通过setTimeout解决子组件不会销毁的问题
防火墙基础之流量管理与控制
Visual Studio 2010 compilation qt5.6.3
C#/VB.NET 将PDF转为Excel
昨日元宇宙| 沃尔玛成立探索元宇宙和Web3的创新部门,Dior发布元宇宙展览
Focus on the 35 year old Kan: fear is because you don't have the ability to match your age
#夏日挑战赛#OHOS构建自定义服务实战
10:00面试,10:02就出来了 ,问的实在是太...
一台服务器最大并发 tcp 连接数多少?65535?
PID控制详解[通俗易懂]
岛屿类问题通用解法与DFS框架
The first place on the list - brake by wire "new cycle", the market competitiveness of local suppliers is TOP10
Introduction to reverse commissioning PE structure details 02/07
论文解读(GCC)《Efficient Graph Convolution for Joint Node RepresentationLearning and Clustering》
今天睡眠质量记录80分
Kiss in the metauniverse! CMU launched VR head display plug-in, reproducing the vivid touch of lips
Etcd visualization tool: an introduction to kstone (I)




![[recommendation system] esmm model of multi task learning (updating)](/img/21/8e38d3903eb1110efc4773edb2d09c.png)




