代码随想录算法训练营第一天| 704. 二分查找、35、34、27. 移除元素

LeeCode 704 二分查找

class Solution {
public:
    // 左闭右开
    int search_b(vector<int>& nums, int target) {
        int s, e;
        s = 0, e = nums.size();
        while(s < e){
            int m = (s + e)/2;
            if (nums[m] < target){
                s = m + 1;
            } else if (nums[m] > target){
                e = m;
            } else {
                return m;
            }
        }
        return -1;
    }
    // 左闭右闭
    int search(vector<int>& nums, int target) {
        int s, e;
        s = 0, e = nums.size() - 1;
        while(s <= e){
            int m = (s + e)/2;
            if (nums[m] < target){
                s = m + 1;
            } else if (nums[m] > target){
                e = m - 1;
            } else {
                return m;
            }
        }
        return -1;
    }
};

 LeeCode 35 搜索插入位置

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        int s, e;
        s = 0, e = nums.size()-1;
        while(s <= e){
            // 防止溢出
            int m = s + (e - s) / 2;
            if (nums[m] < target){
                s = m + 1;
            }else if(nums[m] > target){
                e = m - 1;
            } else{
                return m;
            }
        }
        return s;
    }
};

 LeeCode 34 在排序数组中查找元素的第一个和最后一个位置

class Solution {
public:
    int search(vector<int>& nums, int target, int direct) {
        int s, e;
        s = 0, e = nums.size() - 1;
        while (s <= e){
            int m = s + (e - s) / 2;
            if (nums[m] < target){
                s = m + 1;
            }else if (nums[m] > target){
                e = m - 1;
            }else{
                if (direct == -1){
                    if(m == 0 || nums[m - 1] < target)
                        return m;
                    e = m - 1;
                }else{
                    if(m == nums.size() - 1 || nums[m + 1] > target)
                        return m;
                    s = m + 1;
                }
            }
        }
        return -1;
    }
    vector<int> searchRange(vector<int>& nums, int target) {
        vector<int> res(2);
        res[0] = search(nums, target, -1);
        res[1] = search(nums, target, 1);
        return res;
    }
};

 LeeCode 27 移除元素

class Solution {
public:
    // 暴力
    int removeElement_b(vector<int>& nums, int val) {
        int res = nums.size();
        for(int i = nums.size()-1; i>=0; i--){
            if(nums[i] == val){
                for(int j = i; j < res - 1; j++){
                    nums[j] = nums[j+1];
                }
                res -= 1;
            }
        }
        return res;
    }
    // 快慢双指针(写得不好)
    int removeElement_a(vector<int>& nums, int val) {
        int a, b;
        a = 0, b = 0;
        for(; b < nums.size(); b++){
            if(nums[a] != val){
                a++;
            }
            else if (nums[b] != val){
                nums[a] = nums[b];
                nums[b] = val;
                a++;
            }
        }
        return a;
    }
    // 快慢双指针修改(不需要交换,保留val)
    int removeElement(vector<int>& nums, int val) {
        int a, b;
        a = 0, b = 0;
        for(; b < nums.size(); b++){
            if (nums[b] != val){
                nums[a] = nums[b];
                a++;
            }
        }
        return a;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值