add Two Sum task
This commit is contained in:
35
Two Sum/Solution.java
Normal file
35
Two Sum/Solution.java
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int[] test1 = {2, 7, 11, 15}; int target1 = 9;
|
||||||
|
int[] test2 = {3, 2, 4}; int target2 = 6;
|
||||||
|
int[] test3 = {3, 3}; int target3 = 6;
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println("twoSum( " + Arrays.toString(test1) + ", " + target1 + " ): " + Arrays.toString(twoSum(test1, target1)));
|
||||||
|
System.out.println("twoSum( " + Arrays.toString(test2) + ", " + target2 + " ): " + Arrays.toString(twoSum(test2, target2)));
|
||||||
|
System.out.println("twoSum( " + Arrays.toString(test3) + ", " + target3 + " ): " + Arrays.toString(twoSum(test3, target3)));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static int[] twoSum(int[] nums, int target) {
|
||||||
|
int[] res = new int[2];
|
||||||
|
|
||||||
|
HashMap<Integer, Integer> d = new HashMap<>();
|
||||||
|
|
||||||
|
for (int i = 0; i < nums.length; i++) {
|
||||||
|
var remainderIdx = d.get(target - nums[i]);
|
||||||
|
if (remainderIdx != null) {
|
||||||
|
res[0] = i;
|
||||||
|
res[1] = remainderIdx;
|
||||||
|
return res;
|
||||||
|
} else {
|
||||||
|
d.put(nums[i], i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
Two Sum/a.out
Executable file
BIN
Two Sum/a.out
Executable file
Binary file not shown.
48
Two Sum/solution.c
Normal file
48
Two Sum/solution.c
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int key;
|
||||||
|
int value;
|
||||||
|
int used;
|
||||||
|
} Entry;
|
||||||
|
|
||||||
|
int hash(int key, int size) {
|
||||||
|
return (key % size + size) % size;
|
||||||
|
}
|
||||||
|
|
||||||
|
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
|
||||||
|
|
||||||
|
int tableSize = numsSize * 2;
|
||||||
|
Entry* table = calloc(tableSize, sizeof(Entry));
|
||||||
|
|
||||||
|
int* res = malloc(sizeof(int) * 2);
|
||||||
|
*returnSize = 2;
|
||||||
|
|
||||||
|
for (int i = 0; i < numsSize; i++) {
|
||||||
|
int complement = target - nums[i];
|
||||||
|
int h = hash(complement, tableSize);
|
||||||
|
|
||||||
|
while (table[h].used) {
|
||||||
|
if (table[h].key == complement) {
|
||||||
|
res[0] = i;
|
||||||
|
res[1] = table[h].value;
|
||||||
|
free(table);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
h = (h + 1) % tableSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
h = hash(nums[i], tableSize);
|
||||||
|
while (table[h].used) {
|
||||||
|
h = (h + 1) % tableSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
table[h].used = 1;
|
||||||
|
table[h].key = nums[i];
|
||||||
|
table[h].value = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(table);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
44
Two Sum/solution.cpp
Normal file
44
Two Sum/solution.cpp
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<int> twoSum(vector<int>& nums, int target) {
|
||||||
|
vector<int> res(2);
|
||||||
|
|
||||||
|
unordered_map<int, int> d;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < nums.size(); i++) {
|
||||||
|
if (d.find(target - nums[i]) != d.end()) {
|
||||||
|
res[0] = i;
|
||||||
|
res[1] = d[target - nums[i]];
|
||||||
|
return res;
|
||||||
|
} else {
|
||||||
|
d[nums[i]] = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main (int argc, char *argv[]) {
|
||||||
|
Solution s;
|
||||||
|
|
||||||
|
vector<int> test1 = {2, 7, 11, 15}; int target1 = 9;
|
||||||
|
vector<int> test2 = {3, 2, 4}; int target2 = 6;
|
||||||
|
vector<int> test3 = {3, 3}; int target3 = 6;
|
||||||
|
|
||||||
|
vector<int> res1 = s.twoSum(test1, target1);
|
||||||
|
cout << res1[0] << ' ' << res1[1] << endl;
|
||||||
|
vector<int> res2 = s.twoSum(test2, target2);
|
||||||
|
cout << res2[0] << ' ' << res2[1] << endl;
|
||||||
|
vector<int> res3 = s.twoSum(test3, target3);
|
||||||
|
cout << res3[0] << ' ' << res3[1] << endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
10
Two Sum/solution.py
Normal file
10
Two Sum/solution.py
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
class Solution:
|
||||||
|
def twoSum(self, nums: List[int], target: int) -> List[int]:
|
||||||
|
d: dict = {}
|
||||||
|
|
||||||
|
for i in range(len(nums)):
|
||||||
|
if (target - nums[i]) in d.keys():
|
||||||
|
return [i, d[target - nums[i]]]
|
||||||
|
else:
|
||||||
|
d[nums[i]] = i
|
||||||
|
|
||||||
Reference in New Issue
Block a user