35 Search Insertion position

本文详细解析了使用二分查找算法解决搜索插入位置问题的方法,包括找到大于等于目标值的第一个位置和小于等于目标值的最后一个位置。通过比较不同实现方式的效率与优缺点,为读者提供了一种高效且简洁的解决方案。
public class Solution {
    public int searchInsert(int[] nums, int target) {
        if(nums == null || nums.length == 0) {
            return 0;
        }
        int res = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == target) {
                return i;
            } 
            if (nums[i] < target) {
                continue;
            }
            if (nums[i] > target) {
                res = i;
                return res;
            }
        }
        res = nums.length;
        return res;
    }
}

以上是自己写的

 

参考九章答案:用二分

find the first position >= target

public class Solution {
    public int searchInsert(int[] nums, int target) {
        int start = 0;
        int end = nums.length -1;
        
        while (start + 1 < end) {
            int mid = start + (end - start)/2;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] < target) {
                start = mid;
            } else {
                end = mid;
            }
        }
        
        if (nums[start] >= target) {
            return start;
        } else if (nums[end] >= target) {
            return end;
        } else {
            return end+1;
        }
    }
}

find the last position <= target

public class Solution {
    public int searchInsert(int[] A, int target) {
        int start = 0;
        int end = A.length - 1;
        int mid;
        
        if (target < A[0]) {
            return 0;
        }
        // find the last number less than target
        while (start + 1 < end) {
            mid = start + (end - start) / 2;
            if (A[mid] == target) {
                return mid;
            } else if (A[mid] < target) {
                start = mid;
            } else {
                end = mid;
            }
        }
        
        if (A[end] == target) {
            return end;
        }
        if (A[end] < target) {
            return end + 1;
        }
        if (A[start] == target) {
            return start;
        }
        return start + 1;
    }
}

 

转载于:https://www.cnblogs.com/77rousongpai/p/4521443.html

#include<iostream> using namespace std; #define ERROR NULL typedef int ElemType; //ElemType 为可定义的数据类型,此设为int类型 typedef struct LNode { ElemType data; //结点的数据域 struct LNode *next; //结点的指针域 } LNode, *LinkList; //LinkList为指向结构体LNode的指针类型 bool InitList(LinkList &L);//初始化一个为空的带头结点单链表 LNode* Find( LinkList L, ElemType X ); //在带头节点单链表L中查找值为X的节点,如果找到返回该节点的地址,否则返回NULL bool Insert( LinkList &L, ElemType X, LNode* P ); //将X插入在指针P指向的结点之前,返回true。 P==NULL ,表示要插入的位置是表尾。如果参数P指向非法位置,则打印“Wrong Position for Insertion\n”,返回false; bool Delete( LinkList &L, LNode* P ); //将指针P所指向的元素删除并返回true。若参数P指向非法位置,则打印“Wrong Position for Deletion\n”并返回false。 void DispList(LinkList L); //顺序输出链表每个结点,每个结点元素值以空格符间隔,以换行符结束。 /*在带头节点单链表L中查找值为X的节点,如果找到返回该节点的地址,否则返回NULL */ LNode* Find( LinkList L, ElemType X ) { // 请在这里补充代码,完成本关任务 /********** Begin *********/ /********** End **********/ } /*将X插入在指针P指向的结点之前,返回true。 P==NULL ,表示要插入的位置是表尾。如果参数P指向非法位置,则打印“Wrong Position for Insertion\n”,返回false; */ bool I
最新发布
04-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值