当前位置:网站首页>【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];
}
}

边栏推荐
- 榜单首发——线控制动「新周期」,本土供应商市场竞争力TOP10
- 3. Caller 服务调用 - dapr
- Interviewer: how does the thread pool reuse threads? Do you know? Tell me about it
- AI落地的新范式,就“藏”在下一场软件基础设施的重大升级里
- Knowing these commands allows you to master shell's own tools
- Internet of things cloud convergence Security Guide
- 岛屿类问题通用解法与DFS框架
- Sample explanation of batch inserting data using MySQL bulkloader
- 【MySQL】表连接为什么比子查询快
- Navicat 15 for MySQL
猜你喜欢

Azure Kinect Microsoft camera unity development summary

Knowing these commands allows you to master shell's own tools

RedmiBook Pro 14增强版 打不开台达软件DRAStudio_v1.00.07.52

Focus on the 35 year old Kan: fear is because you don't have the ability to match your age

What are the most powerful small and medium-sized companies in Beijing?

逆向调试入门-PE结构详解02/07

【Golang】安装 iris 的方法

榜单首发——线控制动「新周期」,本土供应商市场竞争力TOP10

Cross cluster deployment of helm applications using karmada

10: 00 interview, came out at 10:02, the question is really too
随机推荐
部门新来了个字节25K出来的,让我见识到了什么是天花板
Naacl 2022 | distillation of machinetranslation SOTA model
Knowing these commands allows you to master shell's own tools
The sadness of software testers is Their own technical ability can not meet the requirements of large manufacturers?
面试官: 线程池是如何做到线程复用的?有了解过吗,说说看
FS2K人脸素描属性识别
A 24-year-old bald programmer teaches you how to continuously integrate and deliver microservice delivery. You can't learn how to cut me off
一台服务器最大并发 tcp 连接数多少?65535?
早晨有些犹豫
【Hot100】2.两数相加
Introduction to deep learning in machine learning
Interviewer: how does the thread pool reuse threads? Do you know? Tell me about it
Etcd visualization tool: an introduction to kstone (I)
【MySQL】表连接为什么比子查询快
知道这几个命令让你掌握Shell自带工具
如何查询数据库中一个表中的所有数据呢?
访中国信通院王蕴韬:数实融合赋能文化产业繁荣发展
Code implementation of gain (4) -- gap dataset missing data filling based on GaN (sequence) [improved version]
软件测试员的悲哀竟是...自己的技术能力不能满足大厂要求?
你好,现在网上炒股开户买股票安全吗?