第一章 数组part01(代码随想录训练营)

就看视频理解了以后,试着自己敲了一遍...(平平无奇的打卡
 二分查找:
class Solution {
public:
    int search(vector& nums, int target) {
        int left = 0;
        int right = nums.size()-1;
        while (left <= right) {
            int mid = (left + (right - left)/2);
            if (nums[mid] > target){
                right = mid - 1;
            } else if (nums[mid] < target){
                left = mid + 1;
            }
            else {
                return mid;
            }
        }
        return -1;
    }
};
移除元素:
class Solution {
public:
    int removeElement(vector& nums, int val) {
        int slow = 0;
        for (int i=0; i

你可能感兴趣的:(算法,leetcode,数据结构)