-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.php
More file actions
35 lines (31 loc) · 718 Bytes
/
TwoSum.php
File metadata and controls
35 lines (31 loc) · 718 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php # https://leetcode.com/problems/two-sum/
/**
* @param Integer[] $nums
* @param Integer $target
* @return Integer[]
*/
function twoSum($nums, $target)
{
$resultado = [];
$bandera = false;
foreach ($nums as $key => $value) {
if ($bandera) {break;}
for ($i = ($key+1) ; $i < count($nums); $i++) {
if ( ($nums[$key] + $nums[$i]) == $target)
{
$resultado[] = $key;
$resultado[] = $i;
$bandera = true;
break;
}
}
}
return $resultado;
}
$nums = [2, 7, 11, 15];
$target = 9;
if ( twoSum($nums,$target) == [0,1] ){
echo "correcto";
}else{
echo "incorrecto";
}