Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

哈希表第一题两数之和的相关表述(存疑) #2634

Open
CY-999 opened this issue Jul 19, 2024 · 3 comments
Open

哈希表第一题两数之和的相关表述(存疑) #2634

CY-999 opened this issue Jul 19, 2024 · 3 comments

Comments

@CY-999
Copy link

CY-999 commented Jul 19, 2024

image
这里关于键值对的说法是不是都有点问题?
key用来存放下标,而value才用来存放元素(值)吧??

@kimoge
Copy link
Contributor

kimoge commented Jul 19, 2024

这样两个key相加等于target的情况下可以直接索引到下标位置,要是key存下标和数组有什么区别,这样怎么优化

@CY-999
Copy link
Author

CY-999 commented Jul 20, 2024

这样两个key相加等于target的情况下可以直接索引到下标位置,要是key存下标和数组有什么区别,这样怎么优化
因为查找所用的find()函数在以键值对为元素的容器中,只能查询特定键值(即key)是否出现,所以key不能用来存放下标
若实在想利用数组并通过key存下标,那你应该还得自己想出一个哈希函数,并确保元素映射到的哈希值不太大且均匀分布(不知道你是不是这个意思。。

@kimoge
Copy link
Contributor

kimoge commented Jul 20, 2024

你要想key存下标对本题算法有什么提升。本题就是想求符合条件的两个元素的下标,使用key存元素、value存下标只需要遍历一遍,使用key存下标你一遍循环能出结果么?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants