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
  • 相关阅读:
    前端入门21-JavaScript的ES6新特性
    redis 数据库安装和基本使用
    django 与 Vue 的结合使用说明
    websocket 与 tornado 的结合
    tornado 模板引擎
    tornado 初解
    ajax post 提交数据和文件
    Session Cookies随笔
    爬虫 scrapy 笔记
    绘制验证码 刷新验证码
  • 原文地址:https://www.cnblogs.com/miaoweiye/p/13475349.html
Copyright © 2011-2022 走看看