Last active
February 17, 2021 23:13
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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 | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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