我是靠谱客的博主 贪玩背包,这篇文章主要介绍LEETCODE 1.Two Sum (python实现),现在分享给大家,希望可以做个参考。

题目:
Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution.

Example:
Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

翻译:
给定一个整形数组和一个整数target,返回2个元素的下标,它们满足相加的和为target。
你可以假定每个输入,都会恰好有一个满足条件的返回结果。


 时间复杂度n^2

def twosum(nums, target):
    for i in xrange(len(nums)):
        for j in xrange(i+1, len(nums)):
            if (nums[i] + nums[j]) == target:
                return [i, j]

时间复杂度n(思考一下)

最后

以上就是贪玩背包最近收集整理的关于LEETCODE 1.Two Sum (python实现)的全部内容,更多相关LEETCODE内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(80)

评论列表共有 0 条评论

立即
投稿
返回
顶部