美文网首页
Two Sum最优解法

Two Sum最优解法

作者: LonnieQ | 来源:发表于2021-12-04 18:57 被阅读0次

链接: https://leetcode.com/problems/two-sum
思路:
建立一个哈希表存放所有数字,迭代访问所有数字, 每次都尝试取出另一个数字的位置,如果成功返回这两个数字位置, 否则将数字的位置存到哈希表。这样能完美解决两个数字相等的情况。时间复杂度和空间复杂度都为o(n). 解决方案基于链接: https://leetcode.com/problems/two-sum/discuss/1610384/Optimal-Solution-Explained
解法:

class Solution:

    def twoSum(self, nums: List[int], target: int) -> List[int]:
        prevMap = {}
        for i, n in enumerate(nums):
            diff = target - n
            if diff in prevMap:
                return [prevMap[diff], i]
            prevMap[n] = i
        return []

另外还有一种方法是排序,然后遍历排序后的数字,对另一个数字进行二分搜索。这种方式时间复杂度为o(nlogn), 空间复杂度为o(n)。

class Solution:

    def binarySearch(self, nums, lower, uppuer, target):
        if lower > uppuer:
            return -1
        mid = lower + int((uppuer - lower) / 2)
        if nums[mid] > target:
            return self.binarySearch(nums, lower, mid - 1, target)
        if nums[mid] < target:
            return self.binarySearch(nums, mid + 1, uppuer, target)
        return mid

    def twoSum(self, nums: List[int], target: int) -> List[int]:
        sorted_numbers = sorted(nums)
        len_nums = len(nums)
        len_nums_minus_1 = len_nums - 1
        for i in range(len_nums):
            left = sorted_numbers[i]
            right = target - left
            index = self.binarySearch(sorted_numbers, i + 1, len_nums_minus_1, right)
            if index != -1:
                left_index = 0
                right_index = 0
                j = 0
                if left != right:
                    while j < len_nums:
                        if nums[j] == left:
                            left_index = j
                        if nums[j] == right:
                            right_index = j
                        j += 1
                    return [left_index, right_index]
                while j < len_nums:
                    if nums[j] == left:
                        left_index = j
                    j += 1
                j = len_nums_minus_1
                while j >= 0:
                    if nums[j] == right:
                        right_index = j
                    j -= 1
                return [left_index, right_index]
                    
        return []

相关文章

  • Two Sum最优解法

    链接: https://leetcode.com/problems/two-sum[https://leetcod...

  • 2018-06-15 LeetCode53

    题目描述 我的解法 O(n)的复杂度,sum[i]=max{sum[i-1] + a[i], a[i]} 最优解法

  • leetcode-javascript-1. 两数之和

    https://leetcode-cn.com/problems/two-sum/ for loop常用解法 从第...

  • Two Sum 的 javascript 解法

    解法一 本解法的思路是看到题目后就能立刻想到的:遍历两遍数组,将数组中的值两两相加,找到和为指定值 target ...

  • Algorithm training(一)

    1. two sum 解法: 2. Reverse Integer (数字 反转输出) 避免不了溢出。。。 3.R...

  • 39+40、Combination Sum、Combinatio

    Combination Sum 解法 Combination Sum 2 解法

  • LeetCode的sum问题

    这里写几个sum问题的总结。首先是leetcode 1:two sum解法很简单,就是哈希表。哈希表的查找速度是O...

  • Two sum 最佳解法

    看了一下java的最優解,感嘆真的技不如人簡單來説就是把當前complement的數字在原來array的位數,存進...

  • Leetcode--Array

    1. Two Sum 用hash可以得到O(n)时间的解法,用python中的enumerate函数,可以获得元素...

  • X Sums

    Two Sum It is not easy to write Two-Sum right for the fir...

网友评论

      本文标题:Two Sum最优解法

      本文链接:https://www.haomeiwen.com/subject/wlhbxrtx.html