[leetcode][PHP实现]Two Sum

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
class solution 
{
    public function twoSum($nums, $target)
    {
        $result = array();
        for ($i = 0; $i < count($nums); $i++)
        {
            for ($j = 0; $j < count($nums); $j++)
            {
                if ($target == $nums[$i] + $nums[$j])
                {
                    $result[0] = $i + 1;
                    $result[1] = $j + 1;
                    return $result;
                }
            }
        }
        return $result;
    }
}
$solution = new Solution();
var_dump($solution->twoSum(array(2,7,11,15), 9));
?>
Licensed under CC BY-NC-SA 4.0