代码随想录Day1|704. 二分查找,27. 移除元素

Leetcode

  • 704. Binary Search
    • Question
    • Important
    • Tips
    • Code
      • target in [left, right)
      • target in [left, right]
  • 27. Remove Element
    • Code
      • Two-pointer technique
      • Two-pointer collision method

704. Binary Search

Question

704.二分查找

Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.

Important

  • Sorted array
  • All the integers are unique

Tips

  • 定义target所在区间,区间不变.
    如果nums.size() = 10, 那么区间为 [0,9],或者 [0,10)

  • 推荐“左闭右开” [left, right)

Code

target in [left, right)

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int left = 0;
        int right = nums.size(); // 数组长度
        while (left < right) {
            int middle = left + ((right - left) >> 1); // 左+半个区间,右移动1位=除以2,+-优先级高
            if (nums[middle] > target) {
                right = middle; // right为右索引+1. 落在区间[left,middle)中
            }
            else if (nums[middle] < target) {
                left = middle + 1; // 落在区间[middle + 1, right)
            }
            else {
                return middle;
            }
        }
        return -1; // not existed
    }
};

target in [left, right]

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int left = 0;
        int right = nums.size() - 1; // target in [left, right]

        while (left <= right) {
            int middle = left + ((right - left) >> 1); // 左区间
            if (nums[middle] > target) {
                right = middle - 1;
            }
            else if (nums[middle] < target) {
                left = middle + 1;
            }
            else {
                return middle;
            }
        }
        return -1; // not existed
    }
};

27. Remove Element

27.Remove Element
Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.

Consider the number of elements in nums which are not equal to val be k, to get accepted, you need to do the following things:

Change the array nums such that the first k elements of nums contain the elements which are not equal to val. The remaining elements of nums are not important as well as the size of nums.
Return k.

Code

Two-pointer technique

C++ code:

class Solution {
 public:
  // 双指针;保持了原order
  int removeElement(vector<int>& nums, int val) {
    // 用于记录不等于val的元素的位置(需要删除)。
    auto it = nums.begin();

    for (auto& item : nums) {
      // 如果当前元素不等于val(需要保留),那么移动到it的位置。
      if (item != val) {
        *it++ = item;  // *it = item, it=it+1;
      }
    }
    // // 删除剩余元素
    nums.erase(it, nums.end());

    return nums.size();
    // //若不删除,可以直接返回it位置的个数。
    // return distance(nums.begin(),it);
  }
};

Two-pointer collision method

version 1:

class Solution {
 public:
  //相向双指针,避免了多次移动元素,改变了数组顺序。
  int removeElement(vector<int>& nums, int val) {
    for (size_t i = 0; i < nums.size();) { //size_t非负数,更安全。
      // 最后面的元素移动过来,覆盖当前==val的元素。
      if (nums[i] == val) {
        // 移动最后一个元素到当前位置,并删除最后一个元素。
        nums[i] = move(nums.back());
        nums.pop_back();
        // 不需要递增,因为,进入下一次循环,需要判断移动到i位置的新元素。
      } else {
        ++i;
      }
    }
    return nums.size();
  }
};

version 2:

class Solution {
 public:
  int removeElement(vector<int>& nums, int val) {
    int left = 0;
    int right= nums.size()-1; //区间[left,right]
    while (left <= right)
    {
        // 将右侧不需要删除的元素,移动到,左侧需要删除的位置。
        if(nums[left] == val){ //左侧发现要删除的元素
            if(nums[right] != val){ //right的值不是要删除的
                nums[left] = nums [right]; // 移动right到left位置
            }
            //right是要删除的值,往左移动-1,不是要删除的也-1.
            --right;
        }else{
            ++left; // left不是要删除的,right不动,更新left
        }
    }
    // nums.erase(nums.begin()+left, nums.end());
    return left; //left最终大于right,即数组的右边界+1。
  }
};

你可能感兴趣的:(数据结构与算法,算法,数据结构,二分法,双指针)