您好,登錄后才能下訂單哦!
35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Here are few examples.[1,3,5,6]
, 5 → 2[1,3,5,6]
, 2 → 1[1,3,5,6]
, 7 → 4[1,3,5,6]
, 0 → 0
程序說明:
如果數組為空,則發(fā)揮位置0,若數組中無大于等于的數,則返回數組長度即可。
int searchInsert(int* nums, int numsSize, int target) { if ( numsSize == 0 ) { return 0; } int cnt; for ( cnt = 0; cnt < numsSize; cnt++ ) { if ( *(nums + cnt) >= target ) { return cnt; } } return numsSize; }
由于數組已經是排序的了,故只需逐個開始比較大小,找到相應的位置返回即可
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。