C# Solution


  • 0
            public int SearchInsert(int[] nums, int target)
            {
                if (nums == null || nums.Length == 0)
                    return 0;
    
                int i = 0, j = nums.Length - 1, k = 0;
    
                while (i <= j)
                {
                    k = i + (j - i) / 2;
    
                    if (nums[k] == target)
                        return k;
                    else if (nums[k] < target)
                        i = k + 1;
                    else
                        j = k - 1;
                }
    
                return nums[k] < target ? k + 1 : k;
            }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.