当前位置:网站首页>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}
}
边栏推荐
- Solution of DM database unable to open graphical interface
- What data does the main account of Zhengda Meiou 4 pay attention to?
- C# 基于MQTTNet的服务端与客户端通信案例
- Simple and practical accounting software, so that accounts can be checked
- Mysql重点难题(2)汇总
- 从数组中找出和为目标的下标
- 关于Steam 教育的知识整理
- Unity particle Foundation
- MySQL table insert Chinese change? Solution to the problem of No
- CubeMx DMA笔记
猜你喜欢
![[common error] the DDR type of FPGA device is selected incorrectly](/img/f3/be66bcfafeed581add6d48654dfe34.jpg)
[common error] the DDR type of FPGA device is selected incorrectly

DMA Porter

How to modify data file path in DM database
![[opencv] image binarization](/img/7e/b56a59ffae3bf6cac9c0bb7e090b85.jpg)
[opencv] image binarization

2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas
![[bus interface] Axi interface](/img/ee/95ade7811ec2c37fb67a77f0b6ae2a.jpg)
[bus interface] Axi interface

Application of intelligent robot in agricultural ecology

Hcip day 17

List of common bugs in software testing
![[high speed bus] Introduction to jesd204b](/img/78/1a0a3672e63058da6d98da95aa3cf2.jpg)
[high speed bus] Introduction to jesd204b
随机推荐
Case sharing | intelligent Western Airport
Online incremental migration of DM database
Rhcsa --- work on the fourth day
[high speed bus] Introduction to jesd204b
初学爬虫-笔趣阁爬虫
leetcode两数相加go实现
I sorted out some basic questions about opencv AI kit.
Promise all()
Gin framework learning code
Flag bits in assembly language: CF, PF, AF, ZF, SF, TF, if, DF, of
Lm09 Fisher inverse transform inversion mesh strategy
Record the bug of unity 2020.3.31f1 once
Mysql重点难题(2)汇总
[bus interface] Axi interface
[quick view opencv] familiar with CV matrix operation with image splicing examples (3)
Practical problem solving ability of steam Education
2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas
Let genuine SMS pressure measurement open source code
Keil compilation code of CY7C68013A
TypeScript类的使用