35. Search Insert Position
創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)與策劃設(shè)計(jì),交口網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:交口等地區(qū)。交口做網(wǎng)站價(jià)格咨詢:028-86922220
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
程序說明:
如果數(shù)組為空,則發(fā)揮位置0,若數(shù)組中無大于等于的數(shù),則返回?cái)?shù)組長(zhǎng)度即可。
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; }
由于數(shù)組已經(jīng)是排序的了,故只需逐個(gè)開始比較大小,找到相應(yīng)的位置返回即可