LeetCode刷题 Day01

742 阅读2分钟

LeetCode刷题 Day01

27. Remove Element

Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The relative order of the elements may be changed.

Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.

Return k after placing the final result in the first k slots of nums.

Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.

Example 1:

Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2,_,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 2.
It does not matter what you leave beyond the returned k (hence they are underscores).

快慢指针:

  • 快慢指针同一起点
  • 快指针持续右移
  • 当快指针不同于target value的时候, 覆盖掉慢指针,慢指针向右移动
  • 最后返回慢指针位置

代码:

/**
 * @param {number[]} nums
 * @param {number} val
 * @return {number}
 */
var removeElement = function(nums, val) {
    let slow = 0;
    
    for (let i = 0; i < nums.length; i++) {
        if (nums[i] !== val) nums[slow++] = nums[i];
    }
    
    return slow;
};

时间复杂度: O(n) 空间复杂度: O(1)


704. Binary Search

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.

You must write an algorithm with O(log n) runtime complexity.

Example 1:

Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4

Example 2:

Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1

左闭右闭:

  • 起始值为 left = 0; right = length - 1;
  • while (left <= right)
  • right = mid - 1 (因为right是要被包含的,但是mid已经被验证不符合条件, 所以要mid - 1)

代码:

   /**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */

var search = function(nums, target) {
    let left = 0;
    let right = nums.length - 1;
    
    while (left <= right) {
        let mid = left + Math.floor((right - left) / 2);
        if (nums[mid] === target) return mid;
        else if (nums[mid] < target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    
    return -1;
};

左闭右开:

  • 起始值为 left = 0; right = length;
  • while (left < right)
  • right = mid (因为right是不被包含的, 所以要用mid)
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var search = function(nums, target) {
    let left = 0;
    let right = nums.length;
    
    while (left < right) {
        let mid = left + Math.floor((right - left) / 2);
        if (nums[mid] === target) return mid;
        else if (nums[mid] < target) {
            left = mid + 1;
        } else {
            right = mid;
        }
    }
    
    return -1;
};

时间复杂度: O(logn) 空间复杂度: O(1)