Skip to content

Commit d59c7fd

Browse files
authored
Create two-sum.php
1 parent 06b2de8 commit d59c7fd

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

PHP/two-sum.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
class Solution {
5+
6+
/**
7+
* @param Integer[] $nums
8+
* @param Integer $target
9+
* @return Integer[]
10+
*/
11+
function twoSum($nums, $target) {
12+
$lookup = array();
13+
foreach ($nums as $i => $x) {
14+
if (isset($lookup[$target -$x])) {
15+
return [$lookup[$target - $x], $i];
16+
}
17+
$lookup[$x] = $i;
18+
}
19+
return [];
20+
}
21+
}

0 commit comments

Comments
 (0)