- 两数之和
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
你可以按任意顺序返回答案。
示例 1:
输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
示例 2:
输入:nums = [3,2,4], target = 6
输出:[1,2]
示例 3:
输入:nums = [3,3], target = 6
输出:[0,1]
提示:
2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
只会存在一个有效答案
进阶:
你可以想出一个时间复杂度小于 O(n2) 的算法吗?
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/two-sum
方法一(直接遍历法):
可以使用for循环遍历两次数组,将内层循环和外层循环的数组元素相加,如果和目标值相等则返回两个数组下标,否则返回空数组。 复杂度分析
时间复杂度:O(N^2),其中N是数组中的元素数量。 最坏情况下数组中任意两个数都要被匹配一次。
空间复杂度:O(1)。
方法二(哈希表法):
方法一过高的时间复杂度来源于寻找target-x时产生的时间复杂度,因此需要加快查询速度,可以采用建立索引的方式,由此想到可以使用哈希表储存遍历时的查询结果。
时间复杂度:O(N),其中N是数组中的元素数量,对于每一个元素x,
可以在O(1)复杂度下找到target-x。
空间复杂度:O(N),N是数组中元素的数量,主要是哈希表的开销。
C++提交内容:
//直接遍历法
class Solution {
public:
vector twoSum(vector& nums, int target) {
int length = nums.size();
for(int i = 0; i < length; ++i){
for(int j = i + 1; j < length; ++j){
if(nums[i] + nums[j] == target){
return {i, j};
}
}
}
return {};
}
};
//哈希表法
class Solution {
public:
vector twoSum(vector& nums, int target) {
unordered_map hashtable;
for(int i = 0; i < nums.size(); ++i){
auto it = hashtable.find(target - nums[i]);
if(it != hashtable.end()){
return {it -> second, i};
}
hashtable[nums[i]] = i;
}
return {};
}
};
Python3提交内容:
# 直接遍历法
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
length = len(nums)
for i in range(length):
for j in range(i+1, length):
if nums[i] + nums[j] == target:
return [i, j]
return []
# 哈希表法
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hashtable = dict()
for i,num in enumerate(nums):
if target - num in hashtable:
return [hashtable[target - num], i]
hashtable[nums[i]] = i
GO提交内容:
//直接遍历法
func twoSum(nums []int, target int) []int {
for i, x:=range nums{
for j:=i + 1; j < len(nums); j++{
if x + nums[j] == target{
return []int{i,j}
}
}
}
return nil
}
//哈希表法
func twoSum(nums []int, target int) []int {
hashTable := map[int]int{}
for i, x := range nums {
if p, ok := hashTable[target-x]; ok {
return []int{p, i}
}
hashTable[x] = i
}
return nil
}
JAVA提交内容:
//直接遍历法
class Solution {
public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length; ++i) {
for (int j = i + 1; j < nums.length; ++j) {
if (nums[i] + nums[j] == target) {
return new int[]{i, j};
}
}
}
return new int[0];
}
}
class Solution {
public int[] twoSum(int[] nums, int target) {
Map hashtable = new HashMap();
for (int i = 0; i < nums.length; ++i) {
if (hashtable.containsKey(target - nums[i])) {
return new int[]{hashtable.get(target - nums[i]), i};
}
hashtable.put(nums[i], i);
}
return new int[0];
}
}
C++完整测试代码:
#include#include using namespace std; class Solution { public: vector twoSum(vector & nums, int target) { int length = nums.size(); for(int i = 0; i < length; ++i){ for(int j = i + 1; j < length; ++j){ if(nums[i] + nums[j] == target){ return {i, j}; } } } return {}; } }; int main() { Solution solution; int temp, target, n; cout << "请输入数组长度:" << endl; cin >> n; vector nums; int a[n]; cout << "请输入数组元素:" << endl; for(int i = 0; i < n; i++){ cin >> temp; a[i] = temp; } for(int i = 0; i < n; i++){ nums.push_back(a[i]); } cout << "请输入两数之和:" << endl; cin >> target; vector answer(2); answer = solution.twoSum(nums, target); if(answer.size() != 0){ cout << "[" << answer[0] << "," << answer[1] << "]"; }else{ cout << "不存在符合条件的两数之和" << endl; } return 0; }



