Skip to content

Instantly share code, notes, and snippets.

@ALEXOTANO
Last active February 17, 2021 23:13
Show Gist options
  • Save ALEXOTANO/670edb199db7e91d07094ac1aaf774fe to your computer and use it in GitHub Desktop.
Save ALEXOTANO/670edb199db7e91d07094ac1aaf774fe to your computer and use it in GitHub Desktop.
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
for(var i = 0; i < nums.length; i++){
var n = nums[i]
let otherNumber = target - n
let found = false;
for(var j = 0; j < nums.length; j++) {
if(j != i && otherNumber == nums[j]){
return [i,j];
}
}
return [0,0]; // not found
}
};
<?php
class Solution {
/**
* @param Integer[] $nums
* @param Integer $target
* @return Integer[]
*/
function twoSum($nums, $target) {
foreach($nums as $pos => $n) {
$otherNumber = $target - $n;
foreach($nums as $pos2 => $n2) {
if($pos != $pos2 && $otherNumber == $n2){
return [$pos,$pos2];
}
}
}
return [0,0]; // not found
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment