Accepted C# Solution - faster than 96%


  • 0
    V

    Not all that pretty but works well. I'm sure I could shorten it but for now it's all good.

        public int SearchInsert(int[] nums, int target) {
            Dictionary<int,int> search = new Dictionary<int,int>();
            for(int i=0; i<nums.Length; i++){
                search.Add(nums[i], i);
            }
            if(search.ContainsKey(target))return search[target];
            else{
                for(int i = 0; i<nums.Length; i++){
                    if(target < nums[i])return i;
                    else if(i == nums.Length-1 && target > nums[i]){
                        return nums.Length;
                    } 
                }
            }
            return 0;
        }
    }

Log in to reply
 

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