C++
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> numToIndex;
for (int i = 0; i < nums.size(); ++i) {
if (numToIndex.count(target - nums[i]))
return {numToIndex[target - nums[i]], i};
numToIndex[nums[i]] = i;
}
throw;
}
};
C++
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
map<int, int> umap;
int difference;
for(int i = 0; i < nums.size(); i++ ){
difference = target - nums.at(i);
if(umap.find(difference) != umap.end()) {
vector<int> v{umap[difference], i};
return v;
} else {
umap[nums.at(i)] = i;
}
}
return vector<int> {};
}
};
C++
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> m;
vector<int> result;
for (int i=0; i<nums.size(); i++) {
if ( m.find(target - nums[i]) == m.end() ) {
m[nums[i]] = i;
}else{
result.push_back(m[target - nums[i]]);
result.push_back(i);
}
}
return result;
}
};
C++
class Solution {
public:
vector<int> twoSum(vector<int> &nums, int target) {
unordered_map<int, int> index_map;
for (int index = 0;; index++) {
auto iter = index_map.find(target - nums[index]);
if (iter != index_map.end())
return vector<int> {index, iter -> second};
index_map[nums[index]] = index;
}
}
};
C
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
*returnSize=2;
int *arr=(int *)malloc((*returnSize)*sizeof(int));
for(int i=0;i<numsSize;i++){
for(int j=i+1;j<numsSize;j++){
if(nums[i]+nums[j]==target){
arr[0]=i;
arr[1]=j;
break;
}
}
}
return arr;
}
JAVA
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> numToIndex = new HashMap<>();
for (int i = 0; i < nums.length; ++i) {
if (numToIndex.containsKey(target - nums[i]))
return new int[] {numToIndex.get(target - nums[i]), i};
numToIndex.put(nums[i], i);
}
throw new IllegalArgumentException();
}
}
JAVA
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] indices = new int[2];
Map<Integer, Integer> map = new HashMap<>();
for (int index = 0; index < nums.length; index++) {
if (map.containsKey(target - nums[index])) {
indices[1] = index;
indices[0] = map.get(target - nums[index]);
return indices;
}
map.put(nums[index], index);
}
return indices;
}
}
JAVA
class Solution {
public int[] twoSum(int[] nums, int target) {
if(nums==null || nums.length<2)
return new int[]{0,0};
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i=0; i<nums.length; i++){
if(map.containsKey(nums[i])){
return new int[]{map.get(nums[i]), i};
}else{
map.put(target-nums[i], i);
}
}
return new int[]{0,0};
}
}
Python
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
x = len(nums)
for i in range(x-1):
for j in range(1,x):
if i == j:
continue
if nums[i] + nums[j] == target:
return [i,j]
Python
class Solution:
def twoSum(self, nums, target):
length = len(nums)
for i in range(length):
for j in range(i + 1, length):
if nums[i] + nums[j] == target:
return [i, j]
Python
class Solution:
def twoSum(self, nums, target):
index_map = {}
for index, num in enumerate(nums):
if target - num in index_map:
return index_map[target - num], index
index_map[num] = index
Python
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
numToIndex = {}
for i, num in enumerate(nums):
if target - num in numToIndex:
return numToIndex[target - num], i
numToIndex[num] = i
javascript
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function (nums, target) {
// Array to store the result
result = [];
// Map to store the difference and its index
index_map = new Map();
// Loop for each element in the array
for (let i = 0; i < nums.length; i++) {
let difference = target - nums[i];
if (index_map.has(difference)) {
result[0] = i;
result[1] = index_map.get(difference);
break;
} else {
index_map.set(nums[i], i);
}
}
return result;
};
GOlang
func twoSum(nums []int, target int) []int {
record := make(map[int]int)
for index, num := range nums {
difference := target - num
if res, ok := record[difference]; ok {
return []int{index, res}
}
record[num] = index
}
return []int{}
}
Kotlin
class Solution {
fun twoSum(nums: IntArray, target: Int): IntArray {
// Array to store result
val result = IntArray(2)
// This map will store the difference and the corresponding index
val map: MutableMap<Int, Int> = HashMap()
// Loop through the entire array
for (i in nums.indices) {
// If we have seen the current element before
// It means we have already encountered the other number of the pair
if (map.containsKey(nums[i])) {
// Index of the current element
result[0] = i
// Index of the other element of the pair
result[1] = map[nums[i]]!!
break
} else {
// Save the difference of the target and the current element
// with the index of the current element
map[target - nums[i]] = i
}
}
return result
}
}
You can ask me any question if you don't understand the solution