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

边栏推荐
- Among US private server setup
- 【初学者必看】vlc实现的rtsp服务器及转储H264文件
- Azure Kinect微软摄像头Unity开发小结
- 3. caller service call - dapr
- Mysql自连接查询「建议收藏」
- Soliciting articles and contributions - building a blog environment with a lightweight application server
- Summer Challenge ohos build custom service practice
- 【力扣】977. 有序数组的平方
- Geoffrey Hinton:我的五十年深度学习生涯与研究心法
- 昨日元宇宙|Meta “元宇宙”部门一季度亏损29.6亿美元,六福珠宝发行数字藏品
猜你喜欢

【Proteus仿真】L297驱动步进电机

Open source technology exchange - Introduction to Chengying, a one-stop fully automated operation and maintenance manager

Azure Kinect微软摄像头Unity开发小结

今天睡眠质量记录80分

防火墙基础之流量管理与控制

The future of platform as code is kubernetes extension

24岁秃头程序员教你微服务交付下如何持续集成交付,学不会砍我

A new 25K byte from the Department showed me what the ceiling is

The Web3.0 era is coming. See how Tianyi cloud storage resources invigorate the system to enable new infrastructure (Part 1)
PostgreSQL enables grouping statistics by year, month, day, week, hour, minute and second
随机推荐
Code implementation of gain (4) -- gap dataset missing data filling based on GaN (sequence) [improved version]
js中订阅发布模式bus
ID卡复制教程(使用T5577卡复制4100卡)
[recommendation system] esmm model of multi task learning (updating)
Visual Studio 2010 compilation qt5.6.3
讲师征集令 | Apache DolphinScheduler Meetup分享嘉宾,期待你的议题和声音!
【MySQL】官网文档学习之查询语句sql注意事项
Redmibook Pro 14 enhanced version cannot open delta software drastudio_ v1.00.07.52
Qt5.5.1 configuring msvc2010 compiler and WinDbg debugger
【高并发基础】MySQL索引优化
Soliciting articles and contributions - building a blog environment with a lightweight application server
Cross cluster deployment of helm applications using karmada
The Web3.0 era is coming. See how Tianyi cloud storage resources invigorate the system to enable new infrastructure (Part 1)
岛屿类问题通用解法与DFS框架
leetcode:22. 括号生成
MySQL auto - Connect Query recommended favorites
Summer Challenge ohos build custom service practice
Opengauss kernel: analysis of SQL parsing process
全球陆续拥抱Web3.0,多国已明确开始抢占先机
leetcode:22. bracket-generating