当前位置:网站首页>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}
}
边栏推荐
- 案例分享|智慧化的西部机场
- Exercise notes 13 (effective letter ectopic words)
- 初学爬虫-笔趣阁爬虫
- 國產全中文-自動化測試軟件Apifox
- CubeMx DMA笔记
- How to configure PostgreSQL 12.9 to allow remote connections
- Interview question: do you know the difference between deep copy and shallow copy? What is a reference copy?
- js面试收藏试题1
- Mapping settings in elk (8) es
- I sorted out some basic questions about opencv AI kit.
猜你喜欢
解析少儿编程中的动手搭建教程
Embedded-c language-8-character pointer array / large program implementation
Realize the function of data uploading
Mathematical knowledge -- understanding and examples of fast power
Ansible installation and use
Pytest learning ----- pytest Interface Association framework encapsulation of interface automation testing
10 minute quick start UI automation ----- puppeter
Getting started with pytest ----- confitest Application of PY
C case of communication between server and client based on mqttnet
数学知识(欧拉函数)
随机推荐
Mysql重点难题(2)汇总
Mathematical knowledge (Euler function)
Case sharing | intelligent Western Airport
Research on the security of ognl and El expressions and memory horse
10 minute quick start UI automation ----- puppeter
Rhcsa --- work on the third day
oracle 存储过程与job任务设置
Precipitate yourself and stay up late to sort out 100 knowledge points of interface testing professional literacy
LM09丨费雪逆变换反转网格策略
Interview question: do you know the difference between deep copy and shallow copy? What is a reference copy?
fastText文本分类
C# 基于MQTTNet的服务端与客户端通信案例
Mapping location after kotlin confusion
Video multiple effects production, fade in effect and border background are added at the same time
正大留4的主账户信息汇总
JS interview collection test question 1
Design and implementation of general interface open platform - (44) log processing of API services
Save the CDA from the disc to the computer
Basic differences between Oracle and MySQL (entry level)
MMAP zero copy knowledge point notes