力扣 35.搜索插入位置(C)

題目就不復制了
今天腦袋比較靈光

int searchInsert(int* nums, int numsSize, int target){
    int result = -1;
    for(int i = 0;i < numsSize; i++){
    	//包含除了目標值比數組末尾值還大的所有情況
        if(nums[i] >= target){
            result = i;
            break;
        }
        //目標值比數組末尾值還大
        else{
            result = numsSize;
        }

    }
    return result;


}

僅供參考
有錯誤歡迎指出,有問題歡迎諮詢

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章