[LeetCode]35.SearchInsertPosition

35. Search Insert Position

創(chuàng)新互聯(lián)是一家專(zhuān)注于成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)與策劃設(shè)計(jì),交口網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(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

程序說(shuō)明:

如果數(shù)組為空,則發(fā)揮位置0,若數(shù)組中無(wú)大于等于的數(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è)開(kāi)始比較大小,找到相應(yīng)的位置返回即可

當(dāng)前題目:[LeetCode]35.SearchInsertPosition
本文地址:http://muchs.cn/article24/pidhce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、用戶體驗(yàn)網(wǎng)站改版、域名注冊(cè)、品牌網(wǎng)站制作、網(wǎng)站設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)