Double pointer of leetcode array

704. Binary Search

link

topic description

insert image description here

topic analysis

The question stem has pointed out that the array is an ascending array, so it can be solved by using the dichotomy method. The only thing to pay attention to is the value of the boundary condition. There are two solutions, left closed and right closed, left closed and right open.
Left closed and right closed, because the loop end condition is left<=right, when mid > right, the right boundary value has already been compared, so mid = right - 1 left closed right open, the loop price adjustment is left < right when mid >
right , the right boundary has not been compared, so mid = right

// 左闭右闭,边界值都是有效值
var search = function(nums, target) {
    let left = 0
    let right = nums.length - 1;
    while(left<=right){
        const mid = (left + right) >> 1  // 位运算右移 相当于除2取整
        if(nums[mid] == target) {
            return mid
        } else if(nums[mid] > target){  // 边界值可取  right = mid-1
            right = mid - 1
        } else {
            left = mid + 1
        }
    }
    return -1
};

// 左闭右开  右边界值无效
var search = function(nums, target) {
    let left = 0
    let right = nums.length ;
    while(left<right){
        const mid = (left + right) >> 1
        if(nums[mid] == target) {
            return mid
        } else if(nums[mid] > target){  // 右边界值无效 则无法 
            right = mid
        } else {
            left = mid + 1
        }
    }
    return -1
};

Experience: Just remember the boundary condition judgment value.

27. Remove elements

topic description

remove element
insert image description here

The array must be modified in place, and no extra space can be used.
Idea 1: Use double pointers to traverse from both ends of the array, assign the data that is not equal to the target value from the right to the data that is equal to the target value from the left, and finally left is not equal to the target The length of the array of values, left = right makes sense. * The inner loop must add left<right constraints, otherwise it will go to the two boundaries of the array

var removeElement = function(nums, val) {
   let left = 0;
   let right = nums.length-1
   while(left <= right){
        while(left <= right &&nums[left] !== val) {
            left ++ ;// 左边开始不等于目标值的直接跳过
        }
        while(left <= right &&nums[right] === val){
            right --;// 右边开始等于目标值的直接跳过
        }
 
        // 此时到了 左边等于目标值了  应该将左右互换
        if(left < right){
            nums[left] = nums[right]; // 将右边的不等于目标值的数赋值给左边
            // 区间缩小
            left ++
            right --
        }
   }
   // 最后左边都是不等于目标值的数据 返回前left个数即可
   return left
};

Idea 2: Double pointers, use the fast and slow pointers, and traverse from the far left of the array at the same time. When the array elements are not equal to the target value, the fast and slow pointers move to the right at the same time, and assign the fast pointer value to the slow pointer; when it is equal to the target value When , only the fast pointer is moved, and the slow pointer remains unchanged. The values ​​of the fast and slow pointers will be different in the next cycle. Finally, all the data that is not equal to the target value are moved to the left, and the value of the slow pointer is the length of the array that is not equal to the target value.

var removeElement = function(nums, val) {
// 快慢指针
  let slowIndex = 0;
  for(let fastIndex = 0;fastIndex<nums.length;fastIndex++) {
      if(val !== nums[fastIndex]) {
          // 当快指针值和目标值不相等时,快慢指针同时更新  当相等时 只更新快指针  下次循环时快慢指针的变得不同 ,将快指针值赋给慢指针
          nums[slowIndex] = nums[fastIndex] 
          slowIndex++
      }
  }
  return slowIndex
};

Guess you like

Origin blog.csdn.net/Salange1/article/details/128226557
Recommended