描述
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.
题目链接:https://leetcode.com/problems/two-sum/description/
分析
这一题最直观的思路就是两层for循环,但是这样时间复杂度是O(n^2)。 因为题目里告诉了只有唯一解,所有我们可以使用hash来做。
代码实现
go实现
1 | func twoSum(nums []int, target int) []int { |
python实现
1 | class Solution(object): |