Leetcode刷题记录本

发布时间 2023-08-09 16:40:28作者: 梁君牧

Leetcode刷题记录本

ID: 1

点击查看代码
  1. 暴力破解法
class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
		# 暴力破解法
        for i in range(len(nums)):
            for j in range(len(nums)):
                if nums[i]+nums[j]==target:
                    return [i,j]
  1. 使用字典,即key-map
class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        records = dict()
        for index,value in enumerate(nums):
            if target-value in records:
                return [records[target-value],index]
            records[value] = index