当前位置:网站首页>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}
}
边栏推荐
- Pit encountered in win11 pytorch GPU installation
- oracle 存储过程与job任务设置
- Analyze the space occupied by the table according to segments, clusters and pages
- Super detailed pycharm tutorial
- Realize the function of data uploading
- 2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas
- Introduction to Luogu 3 [circular structure] problem list solution
- MySQL table insert Chinese change? Solution to the problem of No
- CubeMx DMA笔记
- 奠定少儿编程成为基础学科的原理
猜你喜欢

Go Chan's underlying principles

How to write a client-side technical solution

Application of intelligent robot in agricultural ecology

DMA Porter

Getting started with pytest -- description of fixture parameters

Gin framework learning code

Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market

Let genuine SMS pressure measurement open source code

idea自动导包和自动删包设置

Starting from the classification of database, I understand the map database
随机推荐
正大留4的主账户信息汇总
How to recover deleted data in disk
[high speed bus] Introduction to jesd204b
Analyzing the hands-on building tutorial in children's programming
List of common bugs in software testing
Lay the foundation for children's programming to become a basic discipline
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
函数中使用sizeof(arr) / sizeof(arr[0])求数组长度不正确的原因
农业生态领域智能机器人的应用
Summary of common string processing functions in C language
Design and implementation of general interface open platform - (44) log processing of API services
TypeScript函数详解
[quick view opencv] familiar with CV matrix operation with image splicing examples (3)
Oracle stored procedure and job task setting
Ruby replaces gem Alibaba image
关于Steam 教育的知识整理
DC-1靶场搭建及渗透实战详细过程(DC靶场系列)
Solution of DM database unable to open graphical interface
洛谷入门3【循环结构】题单题解
MySQL table insert Chinese change? Solution to the problem of No