【代码随想录算法训练营第42期 第一天 | LeetCode704. 二分查找、27. 移除元素】

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


一、704. 二分查找

解题代码C++:

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int l = 0, r = nums.size() - 1;
        while(l < r)
        {
            int mid = l + r + 1 >> 1;
            if(nums[mid] <= target) l = mid;
            else r = mid - 1;
        }

        if(nums[r] == target) return r;
        else return -1;
    }
};

题目链接/文章讲解/视频讲解:
https://programmercarl.com/0704.%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE.html



二、27. 移除元素

解题代码C++:

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int slow = 0;
        for(int fast = 0; fast < nums.size(); fast ++)
            if(nums[fast] != val)
                swap(nums[slow ++], nums[fast]);
        return slow;
    }
};

题目链接/文章讲解/视频讲解:
https://programmercarl.com/0027.%E7%A7%BB%E9%99%A4%E5%85%83%E7%B4%A0.html

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 20:08:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 20:08:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 20:08:02       58 阅读
  4. Python语言-面向对象

    2024-07-18 20:08:02       69 阅读

热门阅读

  1. 轻松掌握PyTorch CUDA编程,机器学习如虎添翼

    2024-07-18 20:08:02       20 阅读
  2. 【无标题】Elasticsearch for windows

    2024-07-18 20:08:02       22 阅读
  3. 数据仓库实践:了解和定义指标

    2024-07-18 20:08:02       21 阅读
  4. 互联网摸鱼日报(2024-07-18)

    2024-07-18 20:08:02       22 阅读
  5. 【Unity】RPG2D龙城纷争(十三)升级系统

    2024-07-18 20:08:02       25 阅读
  6. 使用 GO 和 Python 分别写爬虫的区别

    2024-07-18 20:08:02       20 阅读
  7. 数据库系统概论:数据库查询语言 SQL

    2024-07-18 20:08:02       21 阅读
  8. 017.自定义指纹浏览器-传参固定指纹(二)

    2024-07-18 20:08:02       18 阅读
  9. 【时时三省】单元测试 简介

    2024-07-18 20:08:02       21 阅读