leetcode1:两数之和

题目链接

leetcode

题目描述

给定一个整数数组 nums 和一个整数目标值 target,在该数组中找出和为目标值 target 的那两个整数,并返回它们的数组下标。
假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
你可以按任意顺序返回答案。

C++ 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;

void printArray(const vector<int>& nums) {
cout << "[";
for (const int& num : nums) {
cout << num << ",";
}
cout << "]";
}

/*
暴力枚举
两重循环枚举下标 i、j,判断 nums[i] + nums[j] 是否等于 target。

时间复杂度:O(n^2)
空间复杂度:O(1)
*/
class Solution_0 {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> res;
for (int i = 0; i < nums.size(); i++) {
for (int j = 0; j < i; j++) {
if (nums[i] + nums[j] == target) {
res = vector<int> {j, i};
break;
}
}
if (res.size() > 0) {
break;
}
}
return res;
}
};

/*
哈希表

遍历一次数组,每次循环:
判断 target − nums[i] 是否在哈希表;
将 nums[i] 插入哈希表;

时间复杂度:O(n)
空间复杂度:O(n)
*/
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> hash;
for (int i = 0; i < nums.size(); i++) {
int other = target - nums[i];
if (hash.count(other)) {
return { hash[other], i };
}
hash[nums[i]] = i;
}
return {};
}
};

/*
思路同上
*/
class Solution_2 {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> map;
for (int i = 0; i < nums.size(); i++) {
auto iter = map.find(target - nums[i]);
if (iter != map.end()) {
return { iter->second, i };
}
// 没找到匹配对,把访问过的元素和下标加入到map
map.insert(pair<int, int>(nums[i], i));
}
return {};
}
};

int main() {
Solution solution;
vector<vector<int>> testCases = {
{2,7,11,15},
{3,2,4},
{3,3}
};
vector<int> targets = {9, 6, 6};

for (int i = 0; i < testCases.size(); i ++) {
vector<int> nums = testCases[i];
int target = targets[i];
vector<int> indices = solution.twoSum(nums, target);
cout << "Input: ";
printArray(nums);
cout << ", Target: " << target << endl;
cout << "Output: ";
printArray(indices);
cout << endl;
}

return 0;
}

Golang 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main

import "fmt"

/*
两重循环枚举下标 i、j,判断 nums[i] + nums[j] 是否等于 target。

时间复杂度:O(n^2)
空间复杂度:O(1)
*/
func twoSum_0(nums []int, target int) []int {
n := len(nums)
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if nums[i] + nums[j] == target {
return []int{i, j}
}
}
}
return nil
}

/*
使用一个 map,存储数组中的每个数及其索引。
遍历数组中的每个数,计算目标值与当前数的差值,并在 map 中查找该差值。
若找到差值,则返回两个数的索引;
若遍历完整个数组都没有找到,则返回空切片。

时间复杂度:O(n)
空间复杂度:O(n)
*/
func twoSum(nums []int, target int) []int {
hash := make(map[int]int)
for i, num := range nums {
need := target - num
// map 中查找差值
if _, ok := hash[need]; ok {
return []int{hash[need], i}
}
// 将当前数字及其索引存入 map
hash[num] = i
}
// 若未找到满足条件的两个数,则返回空切片
return nil
}

func main() {
// 测试用例
testCases := []struct {
nums []int
target int
expected []int
}{
{[]int{2,7,11,15}, 9, []int{0,1}},
{[]int{3,2,4}, 6, []int{1,2}},
{[]int{3,3}, 6, []int{0,1}},
}

for i, tc := range testCases {
result := twoSum(tc.nums, tc.target)
fmt.Printf("Test Case %d, Input: nums = %v, target = %d\n", i+1, tc.nums, tc.target)
resultStr := fmt.Sprintf("%v", result)
expectedStr := fmt.Sprintf("%v", tc.expected)

if resultStr == expectedStr {
fmt.Printf("Test Case %d, Output: %v, PASS\n", i+1, result)
} else {
fmt.Printf("Test Case %d, Output: %v, FAIL (Expected: %v)\n", i+1, result, tc.expected)
}
}
}

leetcode1:两数之和
https://lcf163.github.io/2023/08/07/leetcode1:两数之和/
作者
乘风的小站
发布于
2023年8月7日
许可协议