Friday, January 3, 2020

[LeetCode][PHP] #35 Search Insert Position

https://leetcode.com/problems/search-insert-position/

class Solution {
    function searchInsert($nums, $target) {
        if (array_search($target, $nums) !== false) {
            return array_search($target, $nums);
        }
        else {
            foreach ($nums as $k => $v) {
                if ($v > $target) {
                    return $k;
                }
                else if (($k+1) == count($nums)) {
                    return $k+1;
                }
            }
        }
    }
}

No comments:

Post a Comment