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

测试写代码 #8

Closed
bg6mgd opened this issue Sep 14, 2024 · 0 comments
Closed

测试写代码 #8

bg6mgd opened this issue Sep 14, 2024 · 0 comments
Labels

Comments

@bg6mgd
Copy link
Owner

bg6mgd commented Sep 14, 2024

class Solution:
    def twoSum(self, nums: list[int], target: int) -> list[int]:
        hash_map = {}
        for i, num in enumerate(nums):
            complement = target - num  # 计算目标值需要的差值
            if complement in hash_map:  # 查找哈希表中是否存在这个差值
                return [hash_map[complement], i]  # 找到匹配项,返回两个索引
            hash_map[num] = i  # 将当前数字和索引存入哈希表

@bg6mgd bg6mgd added the 编程 label Sep 14, 2024
@bg6mgd bg6mgd closed this as completed Sep 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant