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

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


一、704. 二分查找

解题代码C++:

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int len = nums.size();

        int l = 0, r = len - 1;
        while(l < r)
        {
            int mid = l + r >> 1;
            if(nums[mid] >= target) r = mid;
            else l = mid + 1;
        }

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

题目链接/文章讲解/视频讲解:
https://programmercarl.com/%E6%95%B0%E7%BB%84%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html



二、27. 移除元素

解题代码C++:

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int len = nums.size();
        int i, j;

        for(i = 0, j = len - 1; i <= j; i ++)
            if(nums[i] == val)
            {
                swap(nums[i], nums[j]);
                j --;
                i --;
            }
        
        len = j + 1;
        
        return len;
    }
};

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

最近更新

  1. TCP协议是安全的吗?

    2024-05-11 18:50:06       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-11 18:50:06       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-11 18:50:06       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-11 18:50:06       20 阅读

热门阅读

  1. 若依ruoyi-vue前端异常处理

    2024-05-11 18:50:06       8 阅读
  2. Rust - 终端输入、文件读写

    2024-05-11 18:50:06       11 阅读
  3. 【C++】n个一位数能够组成的最大数

    2024-05-11 18:50:06       7 阅读
  4. day 28 第七章 回溯算法

    2024-05-11 18:50:06       8 阅读
  5. 【面试干货】HTTPS 工作原理

    2024-05-11 18:50:06       9 阅读