​​欢迎关注我的公众号 [极智视界],获取我的更多笔记分享​

  ​​和大家一起 coding,享受 coding 的乐趣​

  大家好,我是极智视界。本文分享 0001_两数之和 的多语言解法,包括 C++、C、python、go、java、js。

  leetcode 原题链接:https://leetcode-cn.com/problems/two-sum/

  github 题解链接:https://github.com/Jeremy-J-J/leetcode


文章目录

1、题目描述

  给定一个整数数组 ​​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) 的算法吗?



2、题解

2.1 C++

1> 暴力枚举

  • 执行用时 780 ms,在所有 C++ 提交中击败了 5.27% 的用户
  • 内存消耗 9.9 MB,在所有 C++ 提交中击败了 75.05% 的用户
class Solution {
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 < nums.size(); j++){
if(nums[i] + nums[j] == target && i != j){
res.push_back(i);
res.push_back(j);
return res;
}
}
}
return res;
}
};

2> 暴力枚举

  • 执行用时 272 ms,在所有 C++ 提交中击败了 35.17% 的用户
  • 内存消耗 9.9 MB,在所有 C++ 提交中击败了 76.65% 的用户
// 暴力枚举
// 复杂度分析:
// - 时间复杂度:O(N^2), 其中N是数组中的元素数量,最坏情况下数组中任意两个数都要被匹配一次
// - 空间复杂度:O(1)
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int n = nums.size();
for(int i = 0; i < n; ++i){
for(int j = i + 1; j < n; ++j){
if(nums[i] + nums[j] == target){
return {i, j};
}
}
}
return {};
}
};

3> 哈希表

  • 执行用时 4 ms,在所有 C++ 提交中击败了 99.36% 的用户
  • 内存消耗 10.5 MB,在所有 C++ 提交中击败了 37.21% 的用户
// 哈希表
// 复杂度分析:
// - 时间复杂度:O(N), 其中N是数组中的元素数量。对于每一个元素x, 我们可以O(1)地去寻找target-x
// - 空间复杂度:O(N), 其中N是数组中的元素数量,主要为哈希表的开销。
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target){
unordered_map<int, int> 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 {};
}
};

2.2 C

1> 暴力枚举

  • 执行用时 192 ms,在所有 C 提交中击败了 5.14% 的用户
  • 内存消耗 6.2 MB,在所有 C 提交中击败了 62.90% 的用户
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
int* res = (int*)malloc(2 * sizeof(int));
returnSize[0] = 2;
for(int i = 0; i < numsSize; i++){
for(int j = 0; j < numsSize; j++){
if(nums[i] + nums[j] == target && i != j){
res[0] = i;
res[1] = j;
return res;
}
}
}
return res;
}

2> 暴力枚举

  • 执行用时 92 ms,在所有 C 提交中击败了 52.45% 的用户
  • 内存消耗 6.4 MB,在所有 C 提交中击败了 17.06% 的用户
// 暴力枚举
// 复杂度分析:
// - 时间复杂度:O(N^2), 其中N是数组中的元素数量,最坏情况下数组中任意两个数都要被匹配一次
// - 空间复杂度:O(1)
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
for(int i = 0; i < numsSize; ++i){
for(int j = i + 1; j < numsSize; ++j){
if(nums[i] + nums[j] == target){
int* ret = malloc(sizeof(int) * 2);
ret[0] = i, ret[1] = j;
*returnSize = 2;
return ret;
}
}
}
*returnSize = 0;
return NULL;
}

3> 哈希表

  • 执行用时 8 ms,在所有 C 提交中击败了 97.83% 的用户
  • 内存消耗 7.8 MB,在所有 C 提交中击败了 5.05% 的用户
// 哈希表
// 复杂度分析:
// - 时间复杂度:O(N), 其中N是数组中的元素数量。对于每一个元素x, 我们可以O(1)地去寻找target-x
// - 空间复杂度:O(N), 其中N是数组中的元素数量,主要为哈希表的开销。
struct hashTable{
int key;
int val;
UT_hash_handle hh;
};

struct hashTable* hashtable;

struct hashTable* find(int ikey){
struct hashTable* tmp;
HASH_FIND_INT(hashtable, &ikey, tmp);
return tmp;
}

void insert(int ikey, int ival){
struct hashTable* it = find(ikey);
if(it == NULL){
struct hashTable* tmp = malloc(sizeof(struct hashTable));
tmp->key = ikey, tmp->val =ival;
HASH_ADD_INT(hashtable, key, tmp);
}else{
it->val = ival;
}
}

int* twoSum(int* nums, int numsSize, int target, int* returnSize){
hashtable = NULL;
for(int i = 0; i <numsSize; i++){
struct hashTable* it = find(target - nums[i]);
if(it != NULL){
int* ret = malloc(sizeof(int) * 2);
ret[0] = it->val, ret[1] = i;
*returnSize = 2;
return ret;
}
insert(nums[i], i);
}
*returnSize = 0;
return NULL;
}

2.3 python

1> 暴力枚举

  • 执行用时 6568 ms,在所有 Python3 提交中击败了 5.00% 的用户
  • 内存消耗 15.7 MB,在所有 Python3 提交中击败了 55.33% 的用户
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
res = []
for i in range(len(nums)):
for j in range(len(nums)):
if nums[i] + nums[j] == target and i != j:
res.append(i)
res.append(j)
return res
return res

2> 暴力枚举

  • 执行用时 3392 ms,在所有 Python3 提交中击败了 13.59% 的用户
  • 内存消耗 15.6 MB,在所有 Python3 提交中击败了 65.11% 的用户
# 暴力枚举
# 复杂度分析:
# - 时间复杂度:O(N^2), 其中N是数组中的元素数量,最坏情况下数组中任意两个数都要被匹配一次
# - 空间复杂度:O(1)
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
n = len(nums)
for i in range(n):
for j in range(i + 1, n):
if nums[i] + nums[j] == target:
return [i, j]
return []

3> 哈希表

  • 执行用时 36 ms,在所有 Python3 提交中击败了 89.45% 的用户
  • 内存消耗 16.1 MB,在所有 Python3 提交中击败了 13.01% 的用户
# 哈希表
# 复杂度分析:
# - 时间复杂度:O(N), 其中N是数组中的元素数量。对于每一个元素x, 我们可以O(1)地去寻找target-x
# - 空间复杂度:O(N), 其中N是数组中的元素数量,主要为哈希表的开销。
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
return []

2.4 go

1> 暴力枚举

  • 执行用时 48 ms,在所有 Go 提交中击败了 5.59% 的用户
  • 内存消耗 3.4 MB,在所有 Go 提交中击败了 98.11% 的用户
func twoSum(nums []int, target int) []int {
var res = []int{0, 1}
for i := 0; i < len(nums); i++{
for j := 0; j < len(nums); j++{
if nums[i] + nums[j] == target && i != j{
res[0] = i
res[1] = j
return res
}
}
}
return res
}

2> 暴力枚举

  • 执行用时 28 ms,在所有 Go 提交中击败了 26.41% 的用户
  • 内存消耗 3.4 MB,在所有 Go 提交中击败了 86.87% 的用户
// 暴力枚举
// 复杂度分析:
// - 时间复杂度:O(N^2), 其中N是数组中的元素数量,最坏情况下数组中任意两个数都要被匹配一次
// - 空间复杂度:O(1)
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
}

3> 哈希表

  • 执行用时 4 ms,在所有 Go 提交中击败了 95.26% 的用户
  • 内存消耗 4.1 MB,在所有 Go 提交中击败了 43.05% 的用户
// 哈希表
// 复杂度分析:
// - 时间复杂度:O(N), 其中N是数组中的元素数量。对于每一个元素x, 我们可以O(1)地去寻找target-x
// - 空间复杂度:O(N), 其中N是数组中的元素数量,主要为哈希表的开销。
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
}

2.5 java

1> 暴力枚举

  • 执行用时 120 ms,在所有 Java 提交中击败了 5.04% 的用户
  • 内存消耗 41.3 MB,在所有 Java 提交中击败了 75.33% 的用户
class Solution {
public int[] twoSum(int[] nums, int target){
// int[] res = new int[2];
int[] res = {0, 0};
for(int i = 0; i < nums.length; i++){
for(int j = 0; j < nums.length; j++){
if(nums[i] + nums[j] == target && i != j){
res[0] = i;
res[1] = j;
return res;
}
}
}
return res;
}
}

2> 暴力枚举

  • 执行用时 49 ms,在所有 Java 提交中击败了 38.85% 的用户
  • 内存消耗 41.5 MB,在所有 Java 提交中击败了 53.75% 的用户
// 暴力枚举
// 复杂度分析:
// - 时间复杂度:O(N^2), 其中N是数组中的元素数量,最坏情况下数组中任意两个数都要被匹配一次
// - 空间复杂度:O(1)
class Solution {
public int[] twoSum(int[] nums, int target){
int n = nums.length;
for(int i = 0; i < n; ++i){
for(int j = i + 1; j < n; ++j){
if (nums[i] + nums[j] == target){
return new int[]{i, j};
}
}
}
return new int[0];
}
}

3> 哈希表

  • 执行用时 1 ms,在所有 Java 提交中击败了 99.31% 的用户
  • 内存消耗 41.6 MB,在所有 Java 提交中击败了 38.45% 的用户
// 哈希表
// 复杂度分析:
// - 时间复杂度:O(N), 其中N是数组中的元素数量。对于每一个元素x, 我们可以O(1)地去寻找target-x
// - 空间复杂度:O(N), 其中N是数组中的元素数量,主要为哈希表的开销。
class Solution{
public int[] twoSum(int[] nums, int target){
Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
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];
}
}

2.6 js

1> 暴力枚举

  • 执行用时 136 ms,在所有 JavaScript 提交中击败了 20.02% 的用户
  • 内存消耗 41.4 MB,在所有 JavaScript 提交中击败了 74.67% 的用户
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
var res = [];
for(var i = 0; i < nums.length; i++){
for(var j = 0; j < nums.length; j++){
if(nums[i] + nums[j] == target && i != j){
res[0] = i;
res[1] = j;
return res;
}
}
}
return res;
};

2> 暴力枚举

  • 执行用时 108 ms,在所有 JavaScript 提交中击败了 31.56% 的用户
  • 内存消耗 41.4 MB,在所有 JavaScript 提交中击败了 77.25% 的用户
// 暴力枚举
// 复杂度分析:
// - 时间复杂度:O(N^2), 其中N是数组中的元素数量,最坏情况下数组中任意两个数都要被匹配一次
// - 空间复杂度:O(1)
var twoSum = function(nums, target){
var n = nums.length;
for(var i = 0; i < n; ++i){
for(var j = i + 1; j < n; ++j){
if(nums[i] + nums[j] == target){
return [i, j];
}
}
}
return [];
}

3> 哈希表

  • 执行用时 60 ms,在所有 JavaScript 提交中击败了 92.81% 的用户
  • 内存消耗 42.4 MB,在所有 JavaScript 提交中击败了 21.44% 的用户
// 哈希表
// 复杂度分析:
// - 时间复杂度:O(N), 其中N是数组中的元素数量。对于每一个元素x, 我们可以O(1)地去寻找target-x
// - 空间复杂度:O(N), 其中N是数组中的元素数量,主要为哈希表的开销。
var twoSum = function(nums, target){
const map = new Map()
for(let i = 0; i < nums.length; ++i){
const d = target - nums[i]
if(map.has(d)){
return [map.get(d), i]
}
map.set(nums[i], i)
}
}



  好了,以上分享和整理了 0001_两数之和的多语言解法,希望我的分享能对你的学习有一点帮助。



  好了,以上分享了 taichi 的安装方法和一些好玩的示例展示,希望我的分享能对你的学习有一点帮助。



 【公众号传送】


​《【编程艺术】0001_两数之和_解法》​




【编程艺术】0001_两数之和_解法_golang

扫描下方二维码即可关注我的微信公众号【极智视界】,获取更多AI经验分享,让我们用极致+极客的心态来迎接AI !