Guest User

twoSum-2

a guest
Jun 18th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.44 KB | None | 0 0
  1. class Solution {
  2. public function twoSum(array $nums, int $target) {
  3. $c = count($nums);
  4. #$n = array_combine( array_values($nums), array_keys($nums) );
  5. $n = [];
  6. for($i=0;$i<$c;$i++){
  7. $x = $target - $nums[$i];
  8. if(array_key_exists($x, $n)){
  9. return [ $n[$x], $i ];
  10. }
  11. $n[$nums[$i]] = $i;
  12. }
  13. return [];
  14. }
  15. }
Advertisement
Add Comment
Please, Sign In to add comment