当前位置:网站首页>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 .
边栏推荐
- 规划数学期末模拟考试一
- 【流放之路-第四章】
- Yocto project download and compilation
- Golang run times undefined error [resolved]
- 【7.27】代码源 - 【删数】【括号序列】【数字替换】【游戏】【画画】
- [WesternCTF2018]shrine
- TDA75610-I2C-模拟功放I2C地址的确定
- Leetcode 113: path sum II
- Planning mathematics final simulation exam I
- What are the common cyber threats faced by manufacturers and how do they protect themselves
猜你喜欢

Making high-precision map based on autoware (V)

Autoware reports an error: can't generate global path for start solution
![[the road of Exile - Chapter 5]](/img/ef/7ecc1cb4a95c613f7be91f7acc761c.png)
[the road of Exile - Chapter 5]

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

Super scientific and technological data leakage prevention system, control illegal Internet behaviors, and ensure enterprise information security

Js DOM2 和 DOM3

Wonderful use of data analysis

【golang】使用select {}

MySQL execution order

DSP vibration seat
随机推荐
leetcode/0和1个数相同的连续子数组
Golang startup error [resolved]
【7.21-26】代码源 - 【好序列】【社交圈】【namonamo】
TDA75610-I2C-模拟功放I2C地址的确定
[the road of Exile - Chapter 6]
Use POI to export excel file, image URL to export file, image and excel file to export compressed package
What is the ISO assessment? How to do the waiting insurance scheme
Why does stonedb dare to call it the only open source MySQL native HTAP database in the industry?
The basic concept of transaction and the implementation principle of MySQL transaction
leetcode/和大于等于target的连续最短子数组
[UE4] replay game playback for ue4.26
Sigma-DSP-OUTPUT
把逻辑做在Sigma-DSP中的优化实例-数据分配器
Yocto project download and compilation
【GoLang】同步锁 Mutex
为什么 BI 软件都搞不定关联分析
How companies make business decisions -- with the help of data-driven marketing
Analyzing the function of human-computer interface module of runtime manager based on autoware
golang run时报undefined错误【已解决】
golang启动报错【已解决】