Tuesday, November 26, 2019

[LeetCode][PHP] #1 Two Sum

https://leetcode.com/problems/two-sum/submissions/

class Solution {
    function twoSum($nums, $target) {
        foreach ($nums as $k => $v) {
            unset($nums[$k]);
            if ($k2 = array_search($target - $v, $nums)) {
                return [$k, $k2];
            }
        }
        return [];
    }
}

No comments:

Post a Comment