//O(n) hashmaps C++ Two Sum leetcode
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int,int> map;
for(int i=0; i<=nums.size();i++){
int compliment=target-nums[i];
if(map.count(compliment)==1){
return {map[compliment],i};
}
else{
map[nums[i]]=i;
}
}
return {-1,-1};
}
};
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> indices;
for(int i=0; i<nums.size(); i++) indices[nums[i]] = i;
for(int i=0; i<nums.size(); i++){
int left = target - nums[i];
if (indices.count(left) && indices[left]!=i) return {i, indices[left]};
}
return {};
}
};
let x = 10
let y = 25
print("Sum of x and y = ", (x+y))
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
}
};
class Solution {
public int[] twoSum(int[] nums, int target) {
}
}
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
}
public class Solution {
public int[] TwoSum(int[] nums, int target) {
}
}
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
};
# @param {Integer[]} nums
# @param {Integer} target
# @return {Integer[]}
def two_sum(nums, target)
end
class Solution {
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
}
}
class Solution {
/**
* @param Integer[] $nums
* @param Integer $target
* @return Integer[]
*/
function twoSum($nums, $target) {
}
}
function twoSum(nums: number[], target: number): number[] {
};