My 8ms C++ easy understand code


  • 0
    Z
    class Solution {
    public:
        int searchInsert(vector<int>& nums, int target) {
            int n = nums.size();
            if(nums[0] > target)     return 0;
            if(nums[n-1] < target) return n;
            
            for(int i = 0; i< n;i++)
            {
                if(nums[i] == target)  
                         return i;
                else if ( (i != n ) && target > nums[i] && target < nums[i+1])
                         return i+1;
            }   
        }
    };

Log in to reply
 

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