当前位置:网站首页>Force is brushed buckle problem for the sum of two Numbers
Force is brushed buckle problem for the sum of two Numbers
2022-08-03 19:04:00 【Lanzhou Qianfan】
The Sum of Two Numbers
This question is the first question of Likou, and it is the place where the dream of brushing questions begins.It must not be underestimated, because I am very disheveled.
The title is as follows:
Given an integer array nums and an integer target value target, please find the two integers in the array that are the target value target and return their array indices.You can assume that there will only be one answer for each input.However, the same element in the array cannot be repeated in the answer.You can return answers in any order.
The requirements of the title are given an array, given a target value, and then let you find the sum of two values in the array equal to the target value, and then return their subscripts.
Normally we don't find the target data so quickly.There is usually a traversal process.A move index is added to the following value.Similar to this.Know to find the target value.
Such code is implemented like this.
for (int i = 0; i < nums.length; i++) {for (int i1=i+1; i1 < nums.length; i1++) {if(nums[i]+nums[i1]==target){arr[0]= i;arr[1]=i1;}}}According to this logic, we still use a double for loop. When looking for the target value, we will traverse to the elements that have been traversed before, which is repetition.
For example, 2+7 2+11 2+15 ··· Then we still go through 11, 15 when adding 7 and the following numbers.In this way, with the traversal of the number of searches and the increase in the amount of data, it is inevitably a waste of time and efficiency.
So we try to solve the problem in this way. After we can traverse this element again, we will write it down, and then we will not use it directly next time, which is great.And can correspond to values and subscripts.Now here is the initialization, the arrow is the traversal index move.
At the beginning, we start with 4. The difference between 4 and the target value is 16. There must be no hashmap, so we put 4 and its index into it.
Then continue, when we get here, we go to 7 to find 13, and find that there is just this key in the map, so we just return the vaue corresponding to this key, and the value is the index.Of course, you can do it the other way around.
Think about it, if we still use brute force exhaustion, then is it still a for loop when we reach 7, then it is 4+6 4+7 … 4+13 …
6+13 6+8 … 13+8 13+7 In this way, we have repeated the traversal value. From this small amount of calculation, we can find that we have used 13 5 times in total, and if we use hashmap,We just pass it once, record it, and then we can find it directly later.Is it very convenient.Implementation code
HashMap map = new HashMap<>();for (int i = 0; i < nums.length; i++) {int result = target-nums[i];if(map.containsKey(result)){map.get(result);arr[1]=i;arr[0]=map.get(result);}else {map.put(nums[i],i);}} This efficiency is very high.
Simpleness is one thing, but it is another matter whether the simple questions can be well understood.Simple questions do not have a higher pass rate than difficult questions.
Sometimes the understanding of knowledge is temporarily understood. If the algorithm foundation is not solid, it will be forgotten immediately, and then it will fall into doubt again.Algorithms are based on data structures plus basic grammar knowledge.
边栏推荐
- Online monitoring of UPS power supply and operating environment in the computer room, the solution is here
- MYSQL误删数据恢复
- 不要小看 WebSocket!长连接、有状态、双向、全双工都是王炸技能
- Oracle 脚本实现简单的审计功能
- 201709-3 CCF jason查询 (满分题解)
- 盲僧发现了华点——教你如何使用API接口获取数据
- BinomialTree 二叉树
- PreFixSum前缀和
- Postgresql快照优化Globalvis新体系分析(性能大幅增强)
- if/else或switch替换为Enum
猜你喜欢

2022年最新的Android面试大厂必考174题(附带详细答案)
![[Azure Event Hub] Create Event Hub Consume Client + Custom Event Position with Azure AD Authentication](/img/fe/db506853be08398f815f4e36beee76.png)
[Azure Event Hub] Create Event Hub Consume Client + Custom Event Position with Azure AD Authentication

【WPS-OFFICE-Word】 WPS中样式的运作原理?样式自动更新、自动改变如何处理?样式的管理方法?

OneNote 教程,如何在 OneNote 中设置页面格式?

PHP base notes - NO. 1

基于ck+redash构建MySQL慢日志+审计日志展示平台

Cobalt Strike (CS) 逆向初探

Protobuf Grpc使用异常 类型有未导出的方法,并且是在不同的软件包中定义

Install porterLB

When does MySQL use table locks and when to use row locks?You should know this
随机推荐
微信小程序分享功能
OneNote 教程,如何在 OneNote 中设置页面格式?
ImportError: /lib/libgdal.so.26: undefined symbol: sqlite3_column_table_name
Confused!Ali was abused on the one hand, but was fortunate to be promoted to Huawei's technology, and successfully got the offer, with an annual salary of 40w
JumpServer开源堡垒机完成龙芯架构兼容性认证
安装radondb mysql遇到问题
WEB 渗透之SSRF
Bytes to beat three sides take offer: network + GC + + IO + redis + JVM red-black tree + data structure, to help you quickly into the giant!!!!!
技术开发人员常用的安全浏览器
typescript学习笔记
vulnhub pyexp: 1
基于移动GIS的环保生态管理系统
BinomialTree 二叉树
选出表中的中位数记录[构造左右边界 || 问题转换]
87. (Home of cesium) cesium heat map (topography)
使用安全浏览器将网页保存为pdf的方法步骤
系统太多,多账号互通如何实现?
PHP Basic Notes-NO.2
Postgresql-xl全局快照与GTM代码走读(支线)
阿里巴巴政委体系-第七章、阿里政委培育