zoukankan      html  css  js  c++  java
  • 两数之和(1)

    法一:
    
    class Solution:
        def twoSum(self, nums: List[int], target: int) -> List[int]:
            ret = []
            for i in range(len(nums)):
                for j in range(1,len(nums)-i):
                    if nums[i] + nums[-j] == target:
                        ret.append(i)
                        ret.append(len(nums)-j)
                        return re
    
    法二:
    
    class Solution:
        def twoSum(self, nums: List[int], target: int) -> List[int]:
        
             hashmap = {}
            for ind, num in enumerate(nums):
                hashmap[num] = ind
            for i, num in enumerate(nums):
                j = hashmap.get(target - num)
                if j is not None and i != j:
                    return [i, j]
    法三:
    class Solution(object):
        def twoSum(self, nums, target):
            ret = []
            for i in nums:
                other = target-i
                current_index = nums.index(i)
                if other in nums[current_index+1:]:
                    other_index = nums[current_index+1:].index(other)
                    ret.append(current_index)
                    ret.append(current_index+other_index+1)
                    break
            return ret
  • 相关阅读:
    vue生命周期总结
    Generator的基本用法
    React context基本用法
    盗链
    Linux 黑白界面显示
    nginx 反向代理Apache
    apache+php windows下配置
    正则表达式匹配空行
    列表页条目不刷新删除
    linux终端自定义设置
  • 原文地址:https://www.cnblogs.com/miaoweiye/p/13475349.html
Copyright © 2011-2022 走看看