当前位置:网站首页>Force deduction brush question (1): sum of two numbers
Force deduction brush question (1): sum of two numbers
2022-07-29 02:01:00 【Just for fun】
Force button brush questions (1): Sum of two numbers
1 Problem description
Given an array of integers nums And an integer target value target, Please find... In the array and For the target target the Two Integers , And return their array subscripts . You can assume that each input corresponds to only one answer . however , The same element in the array cannot be repeated in the answer . You can return the answers in any order .
2 Problem analysis
This problem , It is the first question of most of the force deduction questions , Including the author himself . In order to record the process of writing questions , It is hereby recorded in CSDN. For your reference .
First , Give an array , for example , [ 2 , 11 , 15 , 7 ] [2,11,15,7] [2,11,15,7], There are four elements in it , Here we need to find two Spy , We don't know what these two spies are worth , however , know , The sum of the two of them is 9 9 9.
The task now is , Find these two Spy Hiding place !
What shall I do? ?
These four guys line up next to each other , Come and check .
first : 2 2 2, the second : 11 11 11, Third : 15 15 15, The fourth one : 7 7 7.
(1)step 1:
2 2 2, The first one , First put it in a small black room !
(2)step 2:
11 11 11, Second person , Check the , It and the people in the little dark room , Is the sum equal to 9 9 9.
There is only 2 2 2, 11 + 2 = 13 11+2=13 11+2=13, No 9 9 9.
ok , hold 11 11 11 Shut up in a little dark house .
(3)step 3:
15 15 15, The third person , Check the , It and the people in the little dark room , Is the sum equal to 9 9 9.
In the little dark room 2 2 2, 11 11 11, It doesn't add up 9 9 9.
hold 15 15 15 Shut up in a little dark house !
(2)step 4:
7 7 7, The fourth person . Find out ! It is similar to that in the little dark room 2 2 2 The sum is equal to 9 9 9!
These two men are spies !
2 2 2 In position 0 0 0, 7 7 7 In position 3 3 3!
3 python Realization
This is the code :
class Solution:
def twoSum(self, nums, target):
n = len(nums)
hashtable = dict()
for i in range(n):
if target - nums[i] in hashtable:
return [hashtable[target - nums[i]], i]
else:
hashtable[nums[i]] = i
return []
python Medium class Defines a class , be called Solution, It defines a function , be called twoSum.
This function , There are two inputs , named nums and target,nums It is a suspicious person who needs to be searched , It's an array .target Is the only known information .
You may think , Not ah , There's another. self, What is this? ? Ha ha ha , No matter what ,python in , Functions defined in classes , The first parameter is written ,self.
Why? ? Actually , I don't know much , Not a computer major , become a monk or nun late in life , In the future, I will write a small article to explain why I want to write self.
hashtable It's our little black house . At first, there was nothing in the little dark room , It's empty. .
hashtable = dict()
hashtable It's actually a hash table , Or hash table , adopt python Dictionary in , Realization , Specifically, why is it called " Hash ", later , Don't get tangled up !
The following is judgment , Whether the sum of this number and the number in the little black room equals 9 9 9!
if target - nums[i] in hashtable:
This is , Shut up in a little dark house !
hashtable[nums[i]] = i
Okay , That's it !
4 Running results
spyder The program written in the compiler :
The operation results are as follows :
Let's take a look at the running results in the buckle :

Okay , It's done ! Remember to like the collection .
边栏推荐
- Web crawler API Quick Start Guide
- Analyze OP based on autoware_ global_ Planner global path planning module re planning
- [the road of Exile - Chapter 5]
- Autoware reports an error: can't generate global path for start solution
- StoneDB 邀请您参与开源社区月会!
- 九天后我们一起,聚焦音视频、探秘技术新发展
- 秘术冬潮烙技能搭配
- Google Cloud Spanner的实践经验
- Reinforcement learning (II): SARS, with code rewriting
- [web technology] 1395 esbuild bundler HMR
猜你喜欢

Stonedb invites you to participate in the open source community monthly meeting!

Process -- user address space and kernel address space

How companies make business decisions -- with the help of data-driven marketing

LeetCode 112:路径总和

How to protect WordPress website from network attack? It is essential to take safety measures

规划数学期末模拟考试一

JS 定时器setInterval clearInterval 延时器setTimeOut 异步 动画
![[public class preview]: application exploration of Kwai gpu/fpga/asic heterogeneous platform](/img/e7/1d06eba0e50eeb91d2d5da7524f4af.png)
[public class preview]: application exploration of Kwai gpu/fpga/asic heterogeneous platform
![[the road of Exile - Chapter 5]](/img/ef/7ecc1cb4a95c613f7be91f7acc761c.png)
[the road of Exile - Chapter 5]

golang run时报undefined错误【已解决】
随机推荐
golang启动报错【已解决】
leetcode/乘积小于K 的连续子数组的个数
golang run时报undefined错误【已解决】
leetcode/0和1个数相同的连续子数组
Reinforcement learning (I): Q-learning, with source code interpretation
PCL point cloud intensity image
Sigma-DSP-OUTPUT
知道创宇上榜CCSIP 2022全景图多个领域
E-commerce keyword research helps data collection
Yocto project download and compilation
The information security and Standardization Commission issued the draft for comments on the management guide for app personal information processing activities
Six simple techniques to improve the value of penetration testing and save tens of thousands of yuan
把逻辑做在Sigma-DSP中的优化实例-数据分配器
The brutal rule of blackmail software continues, and attacks increase by 105%
移动通信——基于卷积码的差错控制系统仿真模型
Network security litigation risk: four issues that chief information security officers are most concerned about
Sword finger offer special assault edition day 13
StoneDB 邀请您参与开源社区月会!
With the explosive growth of digital identity in 2022, global organizations are facing greater network security
[WesternCTF2018]shrine