Time Leetcode二和问题。我的代码';s的时间复杂度为O(n^2)。我怎样才能使它更简单 类解决方案: def twoSum(自身、nums、目标): i=0 而我不会给你确切的密码。但我可以给你一个算法的想法。 使用HashMap。继续存储目标与当前数字的差值作为键,位置作为值
如果您在Time Leetcode二和问题。我的代码';s的时间复杂度为O(n^2)。我怎样才能使它更简单 类解决方案: def twoSum(自身、nums、目标): i=0 而我不会给你确切的密码。但我可以给你一个算法的想法。 使用HashMap。继续存储目标与当前数字的差值作为键,位置作为值,time,time-complexity,Time,Time Complexity,如果您在hashmap中找到差异,则该条目的值和当前值就是您的答案 算法: class Solution: def twoSum(self, nums, target): i = 0 while i <= (len(nums) - 1): j = i + 1 for c, y in enumerate(nums): if y == (target - nums[
hashmap
中找到差异,则该条目的值和当前值就是您的答案
算法:
class Solution:
def twoSum(self, nums, target):
i = 0
while i <= (len(nums) - 1):
j = i + 1
for c, y in enumerate(nums):
if y == (target - nums[i]) and i != c:
return [i, c]
i += 1
for each number
int diff = target - number
if(map.contains(diff))
return map.get(value), current index
else
store the diff, index