两数之和 Two Sum

博客介绍了LeetCode上的两数之和问题,提供了暴力求解和利用哈希表两种方法。暴力求解通过两层循环找到目标和,时间复杂度为O(N^2)。而哈希表方法则将每个数及其索引存入表中,检查目标值减当前数是否已存在,时间复杂度降低到O(N)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

  • mode:easy
  • tag: array(数组)

暴力解决

对于数组中每个数,遍历后面的数,看是否有满足条件的。

  • 时间复杂度: O ( N 2 ) O(N^2) O(N2)
  • 空间复杂度: O ( N ) O(N) O(N)

Python

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        for i in range(len(nums))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值