【Leetcode打卡Day1】two sum总结

这篇博客总结了LeetCode中与两数之和相关的题目,包括基础的two sum,排序数组的two sum,设计数据结构的two sum以及在BST中找两数之和。博主分享了暴力求解和哈希表优化等方法,探讨了时间复杂度和空间复杂度的权衡。
摘要由CSDN通过智能技术生成

【写在前面:还在上学,七月入职,实在太闲,小记一下,菜猫一只,欢迎交流。】

今天第一天,从第一题刷起,大名鼎鼎的two sum,在这里总结一下所有变形题。

【1. two sum】

解法:

1. brute force: loop through each element x by using two for loop.

  • time complexity: O(n^2), n is the length of the array
  • space complexity: O(1)
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        for i in range(len(nums)):
            for j in range(i+1, (len(nums))):
                if nums[i]+nums[j] == target:
                    return [i,j]

2. Hash table

  • time complexity: O(n), n is the length of the array
  • space complexity: O(n), for the hash table, n is the length of the array
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        m &
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值