当前位置:网站首页>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.
边栏推荐
- How many steps does it take to convert an ENS domain name into music?
- SQL function TO_DATE (1)
- 不要再使用MySQL online DDL了
- 123123123123
- Redis的内存淘汰策略和过期删除策略的区别是什么
- C#/VB.NET: extracted from the PDF document all form
- Live chat system technology (8) : vivo live IM message module architecture practice in the system
- Win11怎么安装语音包?Win11语音包安装教程
- log factory (detail)
- Prometheus's Recording rules practice
猜你喜欢
随机推荐
[Server data recovery] Data recovery case of offline multiple disks in mdisk group of server Raid5 array
硬件大熊原创合集(2022/07更新)
有点奇怪!访问目的网址,主机能容器却不行
重保特辑|拦截99%恶意流量,揭秘WAF攻防演练最佳实践
小白系统初始化配置资源失败怎么办
odoo+物联网
Tencent Cloud Hosting Security x Lightweight Application Server | Powerful Joint Hosting Security Pratt & Whitney Version Released
[Kapok] #Summer Challenge# Hongmeng mini game project - Sudoku (3)
开源视界 | StreamNative 盛宇帆:和浪漫的人一起做最浪漫的事
cf:D. Magical Array【数学直觉 + 前缀和的和】
ExcelPatternTool: Excel表格-数据库互导工具
在GBase 8c数据库后台,使用什么样的命令来对gtm、dn节点进行主备切换的操作?
Hardware Bear Original Collection (Updated 2022/07)
Become a Contributor in 30 minutes | How to participate in OpenHarmony's open source contributions in multiple ways?
mysql函数的作用有哪些
C#/VB.NET 从PDF中提取表格
Break the performance ceiling!AsiaInfo database supports more than 1 billion users, with a peak of one million transactions per second
【木棉花】#夏日挑战赛# 鸿蒙小游戏项目——数独Sudoku(3)
【pyqt5】自定义控件 实现能够保持长宽比地缩放子控件
明尼苏达大学团队结合高通量实验与机器学习,实现有效可预测的特定位点重组过程,可调节基因编辑速度










![DAO development tutorial [WEB3.0]](/img/fa/4406317241973d15d776c4f2f0890d.png)