当前位置:网站首页>Find the sum of two numbers
Find the sum of two numbers
2022-08-01 19:06:00 【Lanzhou Qianfan】
Force is brushed buckle problem for the sum of two Numbers
This question is the first question,This is where the dream of brushing questions begins.must not be underestimated,因为我很菜.
题目如下:
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标.
你可以假设每种输入只会对应一个答案.但是,数组中同一个元素在答案里不能重复出现.
你可以按任意顺序返回答案.
Subject requirement is given an array,给定一个目标值,then lets you find the sum of the two values in the array equal to the target value,Then the index to return them.
Usually we don't find the target data so quickly.There is usually a traversal process.A mobile index and the back of the value added.类似于这样.Know to find the target value.
This code is true.
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;
}
}
}
按照这样的逻辑的话,We still use doublefor循环,In looking for the target,We will traverse to the elements we have traversed before,这就是重复.
比如2+7
2+11
2+15
····
然后我们在使用7It will still be traversed when adding to the following numbers.11,15.
In this way, with the number of searches traversed and the amount of data2增多,It is inevitably a waste of time and efficiency.
So we try to solve the problem like this,Can you traverse to this element again?,We'll write down his,Then next time you use it, don't use it directly,这样多好.And can correspond to values and subscripts.
Now here is the initialization,The arrow is to traverse the index to move.

We start with4开始,4difference from target value16,hashmapthere must be no,所以我们把4and its index into it.
然后继续,When we get here we go7去查找13,发现mapIt happens to be this insidekey,So we just put thiskey对应的vaue返回,value就是索引.Of course you to is not the problem.

想想,If still USES violence exhaustive,So is it here?7我们还是for循环
那么就是
4+6
4+7
…
4+13
…
6+13
6+8
…
13+8
13+7
这样去找,We did iterate over the values,From this small amount of calculation, it can be found that we have shared5次13,而我们如果用hashmap,we only go through it once,record it,Then you can find it directly.是不是很方便.
实现代码
HashMap<Integer, Integer> 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);
}
}
这个效率是非常高的.

Simplicity is one aspect,Whether a simple question can be understood properly is another matter..
Simple questions are not as difficult as difficult questions, and the pass rate is high.
Sometimes understanding knowledge temporary understanding,If the algorithm foundation is not solid,will immediately forget,And again in doubt.Algorithms are based on data structures plus basic grammar knowledge.
边栏推荐
- kubernetes-部署nfs存储类
- C#/VB.NET: extracted from the PDF document all form
- Write code anytime, anywhere -- deploy your own cloud development environment based on Code-server
- Heavy cover special | build the first line of defense, cloud firewall offensive and defensive drills best practices
- The life cycle and scope
- Zabbix6.0钉钉机器人告警
- MySQL数据库————流程控制
- The elder brother of the goldfish RHCA memoirs: CL210 experiment management it network - chapter
- To drive efficient upstream and downstream collaboration, how can cross-border B2B e-commerce platforms release the core value of the LED industry supply chain?
- Zabbix6.0 DingTalk robot alarm
猜你喜欢

生命周期和作用域

【综述专栏】IJCAI 2022 | 图结构学习最新综述:研究进展与未来展望

shell脚本专题(07):文件由cfs到bos

No need to crack, install Visual Studio 2013 Community Edition on the official website

【蓝桥杯选拔赛真题47】Scratch潜艇游戏 少儿编程scratch蓝桥杯选拔赛真题讲解

Screen: GFF, OGS, Oncell, Incell of full lamination process

将ENS域名转化为音乐需要几步?

Keras deep learning practice - traffic sign recognition

硬件大熊原创合集(2022/07更新)

网站建设流程
随机推荐
Go GORM transaction instance analysis
SENSORO成长伙伴计划 x 怀柔黑马科技加速实验室丨以品牌力打造To B企业影响力
Stop using MySQL online DDL
Heavy cover special | build the first line of defense, cloud firewall offensive and defensive drills best practices
ExcelPatternTool: Excel form-database mutual import tool
MySQL database - stored procedures and functions
LeetCode 1374.生成每种字符都是奇数个的字符串
【LeetCode】Day109-最长回文串
Summer vacation first week wrap-up blog
C#/VB.NET 从PDF中提取表格
【神经网络】一文带你轻松解析神经网络(附实例恶搞女友)
Write code anytime, anywhere -- deploy your own cloud development environment based on Code-server
Industry Salon Phase II丨How to enable chemical companies to reduce costs and increase efficiency through supply chain digital business collaboration?
Screen: GFF, OGS, Oncell, Incell of full lamination process
Clip-on multimeter use method, how to measure the voltage, current, resistance?
想随时、随地、随心使用数据库的朋友们,全体注意!
kubernetes - deploy nfs storage class
In the background of the GBase 8c database, what command is used to perform the master-slave switchover operation for the gtm and dn nodes?
无需破解,官网安装Visual Studio 2013社区版
对于web性能优化我有话说!
