-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.php
35 lines (31 loc) · 847 Bytes
/
Solution.php
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
declare(strict_types=1);
namespace Konorlevich\Leetcode\TwoSum;
/**
* Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
*
* You may assume that each input would have exactly one solution, and you may not use the same element twice.
*
* You can return the answer in any order.
*
* Only one valid answer exists.
*/
class Solution
{
/**
* @param Integer[] $nums
* @param Integer $target
* @return Integer[]
*/
function twoSum(array $nums, int $target): array
{
foreach ($nums as $k => $v) {
foreach (array_slice($nums, $k + 1, null, true) as $k1 => $v1) {
if (($v + $v1) === $target) {
return [$k, $k1];
}
}
}
return [];
}
}