当前位置:网站首页>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 .
边栏推荐
- What are the common cyber threats faced by manufacturers and how do they protect themselves
- 抓包工具Charles使用
- Top network security prediction: nearly one-third of countries will regulate blackmail software response within three years
- 数学建模——带相变材料的低温防护服御寒仿真模拟
- LeetCode 112:路径总和
- [WesternCTF2018]shrine
- 【7.21-26】代码源 - 【好序列】【社交圈】【namonamo】
- Add graceful annotations to latex formula; "Data science" interview questions collection of RI Gai; College Students' computer self-study guide; Personal firewall; Cutting edge materials / papers | sh
- 【MySQL】sql给表起别名
- Sigma-DSP-OUTPUT
猜你喜欢
随机推荐
Analyzing the function of human-computer interface module of runtime manager based on autoware
golang启动报错【已解决】
DSP震动座椅
leetcode/和为k的连续子数组个数
Autoware reports an error: can't generate global path for start solution
Stonedb invites you to participate in the open source community monthly meeting!
golang run时报undefined错误【已解决】
[golang] network connection net.dial
LeetCode 112:路径总和
Yocto project download and compilation
In depth analysis of C language memory alignment
[7.27] code source - [deletion], [bracket sequence], [number replacement], [game], [painting]
Planning mathematics final simulation exam I
【golang】使用select {}
5g commercial third year: driverless "going up the mountain" and "going to the sea"
JS 定时器setInterval clearInterval 延时器setTimeOut 异步 动画
Six noteworthy cloud security trends in 2022
数学建模——仓内拣货优化问题
数学建模——带相变材料的低温防护服御寒仿真模拟
How to protect WordPress website from network attack? It is essential to take safety measures
![Golang startup error [resolved]](/img/9c/c4757c73d4acd8edf22afa8107fb66.png)






