当前位置:网站首页>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.
边栏推荐
- 基于flowable的upp(统一流程平台)运行性能优化
- Choosing the right DevOps tool starts with understanding DevOps
- Win11如何删除升级包?Win11删除升级包的方法
- 如何看待腾讯云数据库负责人林晓斌借了一个亿炒股?
- 【pyqt5】自定义控件 实现能够保持长宽比地缩放子控件
- Goldfish Brother RHCA Memoirs: CL210 manages OPENSTACK network -- network configuration options
- WinRAR | 将多个安装程序生成一个安装程序
- How to install voice pack in Win11?Win11 Voice Pack Installation Tutorial
- [Neural Network] This article will take you to easily analyze the neural network (with an example of spoofing your girlfriend)
- C#/VB.NET: extracted from the PDF document all form
猜你喜欢

MLX90640 红外热成像仪测温模块开发笔记(完整篇)

GZIPOutputStream 类源码分析

硬件大熊原创合集(2022/07更新)
![[Kapok] #Summer Challenge# Hongmeng mini game project - Sudoku (3)](/img/8d/4f5f7c2463b781cba1c68370d3c29c.png)
[Kapok] #Summer Challenge# Hongmeng mini game project - Sudoku (3)

Zabbix6.0 DingTalk robot alarm

483-82(23、239、450、113)

odoo 编码规范(编程规范、编码指南)

MySQL数据库————存储过程和函数

Keras deep learning practice - traffic sign recognition

Prometheus的Recording rules实践
随机推荐
Zabbix6.0钉钉机器人告警
How to record and analyze your alchemy process - use notes of the visual artifact Wandb [1]
Selenium在远程中的截图
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?
生命周期和作用域
A simple Flask PIN
Win11如何删除升级包?Win11删除升级包的方法
[Kapok] #Summer Challenge# Hongmeng mini game project - Sudoku (3)
Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021:解读
Win11怎么安装语音包?Win11语音包安装教程
Map by value
MySQL database - stored procedures and functions
7月30号|来一场手把手助您打造智能视觉新爆款的技术动手实验
重保特辑|筑牢第一道防线,云防火墙攻防演练最佳实践
对于web性能优化我有话说!
消息模板占位符的使用
暑假第二周总结博客
AntDB database appeared in the 24th high-speed exhibition, helping smart high-speed innovative applications
深入浅出Flask PIN
modbus总线模块DAM-8082
