We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
20170713
https://leetcode.com/problems/two-sum
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, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
class Solution(object): def twoSum1(self, nums, target): """最简单的办法,时间复杂度 O(n^2) :type nums: List[int] :type target: int :rtype: List[int] """ for k,v in enumerate(nums): for i,j in enumerate(nums[k+1:]): if v + j == target: return [k, i+k+1] def twoSum2(self, nums, target): """时间复杂度为 O(n)的办法:Runtime: 76 ms :type nums: List[int] :type target: int :rtype: List[int] """ d = {} for k,v in enumerate(nums): if v not in d: d[target - v] = k else: return [d[v], k]
The text was updated successfully, but these errors were encountered:
No branches or pull requests
题目
https://leetcode.com/problems/two-sum
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, and you may not use the same element twice.
Example:
思路
解答
The text was updated successfully, but these errors were encountered: