当前位置:网站首页>Find the subscript with and as the target from the array
Find the subscript with and as the target from the array
2022-07-02 05:00:00 【Jimmy_ jimi】
Problem description
Given an array of integers nums And an integer target value target, Please find... In the array And is the target value 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 .
Solution :
This is the choice hash Table improves the method of finding the corresponding number , because hashmap The search complexity of is O(1).
Why record this , It's because I just came into contact go, There are two ways of writing that are more meaningful .
package main
import "fmt"
func twoSum(nums []int, target int) []int {
hashTable := map[int]int{
}
for i, x := range nums {
if p, ok := hashTable[target-x]; ok {
fmt.Println(p,i)
return []int{
p, i}
}
//hashmap in key Is the value ,value Subscript the corresponding array
// Find the desired subscript by value
hashTable[x] = i
}
return nil
}
func main() {
nums := []int{
1,2,3,4,5,10}
target := 11
twoSum(nums,target)
}
for i, x := range nums {
// Detect whether there is a key value in the mapping :target-x In mapping hashTable Of map Is there any value in ?
if p, ok := hashTable[target-x]; ok {
return []int{
p, i}
}
边栏推荐
- Analyze the space occupied by the table according to segments, clusters and pages
- Realize the function of data uploading
- Analyzing the hands-on building tutorial in children's programming
- Introduction to Luogu 3 [circular structure] problem list solution
- 解决:代理抛出异常错误
- Preparation for writing SAP ui5 applications using typescript
- 正大美欧4的主账户关注什么数据?
- Lm09 Fisher inverse transform inversion mesh strategy
- Ruby replaces gem Alibaba image
- Mapping settings in elk (8) es
猜你喜欢
DC-1靶场搭建及渗透实战详细过程(DC靶场系列)
Research on the security of ognl and El expressions and memory horse
数据库问题汇总
Here comes the chicken soup! Keep this quick guide for data analysts
How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer
Mathematical knowledge (Euler function)
Idea automatic package import and automatic package deletion settings
Embedded-c language-8-character pointer array / large program implementation
Solution of DM database unable to open graphical interface
Promise all()
随机推荐
Gin framework learning code
Unity particle Foundation
I sorted out some basic questions about opencv AI kit.
TypeScript函数详解
Summary of common string processing functions in C language
Ansible installation and use
Flag bits in assembly language: CF, PF, AF, ZF, SF, TF, if, DF, of
Virtual machine installation deepin system
Application d'un robot intelligent dans le domaine de l'agroécologie
LM09丨费雪逆变换反转网格策略
[Yu Yue education] autumn 2021 reference materials of Tongji University
Mapping location after kotlin confusion
Here comes the chicken soup! Keep this quick guide for data analysts
[high speed bus] Introduction to jesd204b
Typescript function details
Analyze the space occupied by the table according to segments, clusters and pages
Save the CDA from the disc to the computer
Record the bug of unity 2020.3.31f1 once
Fasttext text text classification
[opencv] image binarization