当前位置:网站首页>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}
}
边栏推荐
- DMA Porter
- 06 decorator mode
- Mysql重点难题(2)汇总
- 2022阿里巴巴全球数学竞赛 第4题 虎虎生威(盲盒问题、集卡问题)解决思路
- Pytest learning ----- pytest Interface Association framework encapsulation of interface automation testing
- Precipitate yourself and stay up late to sort out 100 knowledge points of interface testing professional literacy
- Analyze the space occupied by the table according to segments, clusters and pages
- How to configure PostgreSQL 12.9 to allow remote connections
- leetcode两数相加go实现
- Introduction to Luogu 3 [circular structure] problem list solution
猜你喜欢

How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer

Ansible installation and use

Steam教育的实际问题解决能力

正大美欧4的主账户关注什么数据?

Summary of database problems

Future trend of automated testing ----- self healing technology

洛谷入门3【循环结构】题单题解

Precipitate yourself and stay up late to sort out 100 knowledge points of interface testing professional literacy

June book news | 9 new books are listed, with a strong lineup and eyes closed!

Unity particle Foundation
随机推荐
VMware installation win10 reports an error: operating system not found
2022-003arts: recursive routine of binary tree
Oracle和MySQL的基本区别(入门级)
Precipitate yourself and stay up late to sort out 100 knowledge points of interface testing professional literacy
Go GC garbage collection notes (three color mark)
Typescript function details
Promise all()
How to recover deleted data in disk
06 装饰(Decorator)模式
Vmware安装win10报错:operating system not found
Cubemx DMA notes
農業生態領域智能機器人的應用
Rhcsa --- work on the third day
Unity particle Foundation
win10 磁盘管理 压缩卷 无法启动问题
Cannot activate CONDA virtual environment in vscode
Analyzing the hands-on building tutorial in children's programming
Steam教育的实际问题解决能力
UNET deployment based on deepstream
数据库问题汇总