当前位置:网站首页>从数组中找出和为目标的下标
从数组中找出和为目标的下标
2022-07-02 04:58:00 【Jimmy_jimi】
问题描述
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
解决方案:
这是选择hash表提高查找对应数字的方法,因为hashmap的查找复杂度为O(1).
为什么记录这个,是因为刚接触go,有两个比较觉得有意义的书写方式.
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中key为数值,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 {
//检测映射中是否存在一个键值:target-x在映射hashTable的map中是否有值?
if p, ok := hashTable[target-x]; ok {
return []int{
p, i}
}
边栏推荐
- Mysql database learning
- Change deepin to Alibaba image source
- Learn AI safety monitoring project from zero [attach detailed code]
- What data does the main account of Zhengda Meiou 4 pay attention to?
- Super detailed pycharm tutorial
- Common errors of dmrman offline backup
- Research on the security of ognl and El expressions and memory horse
- Several methods of capturing packets under CS framework
- 奠定少儿编程成为基础学科的原理
- Design and implementation of general interface open platform - (44) log processing of API services
猜你喜欢

Thinkphp Kernel wo system source Commercial Open source multi - user + multi - Customer Service + SMS + email notification

Express logistics quick query method, set the unsigned doc No. to refresh and query automatically

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

MySQL table insert Chinese change? Solution to the problem of No

Learn what definitelytyped is through the typescript development environment of SAP ui5

Summary of common string processing functions in C language

ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification

Line by line explanation of yolox source code of anchor free series network (7) -- obj in head_ loss、Cls_ Loss and reg_ Calculation and reverse transmission of loss I

Unity particle Foundation

UNET deployment based on deepstream
随机推荐
正大美欧4的主账户关注什么数据?
Cache consistency solution - how to ensure the consistency between the cache and the data in the database when changing data
10 minute quick start UI automation ----- puppeter
Pytest learning ----- pytest assertion of interface automation testing
Orthogonal test method and function diagram method for test case design
数学知识(欧拉函数)
Oracle和MySQL的基本区别(入门级)
[graduation season · advanced technology Er] young people have dreams, why are they afraid of hesitation
Flag bits in assembly language: CF, PF, AF, ZF, SF, TF, if, DF, of
Social media search engine optimization and its importance
A new attribute value must be added to the entity entity class in the code, but there is no corresponding column in the database table
Thinkphp Kernel wo system source Commercial Open source multi - user + multi - Customer Service + SMS + email notification
國產全中文-自動化測試軟件Apifox
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer
AcrelEMS高速公路微电网能效管理平台与智能照明解决方案智慧点亮隧道
DJB Hash
记录一次Unity 2020.3.31f1的bug
洛谷入门3【循环结构】题单题解
面试会问的 Promise.all()