第一次题解:
            
            
              python
              
              
            
          
          class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        #思路是for循环第一个数然后在数组中找差值
        for i in range(0,len(nums)-1):
            second_value=target-nums[i]
            for j in range(i+1,len(nums)):
                if nums[j]==second_value:
                    return [i,j]思路:两个for循环,找元素
还有一种方法:
            
            
              python
              
              
            
          
          class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        n = len(nums)
        for i in range(0,n):
            for j in range(i+1,n):
                if nums[i] + nums[j] == target:
                    return [i, j]也是两层for循环
使用哈希表解法:
            
            
              python
              
              
            
          
          class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        hashtable={}
        for index,value in enumerate(nums):
            # in hashtable 判断的是键集合
            if target-value not in hashtable:
                # 因为本题返回下标,所以键放数值,值放下标
                hashtable[value]=index
            else:
                return [hashtable[target-value],index]时间复杂度:n
这里有两个要注意的点:
1.enumerate():
返回可迭代的index和values
可以使用enumerate的是:列表,元组和字符串
2. x in hashtable:
这里比较的是x是不是在hashtable的键集合中