Code random thoughts day1 leetcode704, 35, 34, 69, 27, 26

Basics of Array Theory

An array is a collection of data of the same type stored in a contiguous memory space.

Two things to note are

  • Array subscripts start from 0.
  • The addresses of the array memory space are continuous

It is precisely because the addresses of the array in the memory space are continuous, so when we delete or add elements, it is inevitable to move the addresses of other elements.

If you use C++, you should pay attention to the difference between vector and array. The underlying implementation of vector is array. Strictly speaking, vector is a container, not an array.

The elements of the array cannot be deleted, only overwritten.

Is the space address of the two-dimensional array in the memory continuous?

The memory management of different programming languages ​​is different. Taking C++ as an example, two-dimensional arrays are distributed continuously in C++.

leetcode 704 binary search

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int left = 0;
        int right = nums.size() - 1; // 定义target在左闭右闭的区间里,[left, right]
        while (left <= right) { // 当left==right,区间[left, right]依然有效,所以用 <=
            int middle = left + ((right - left) / 2);// 防止溢出 等同于(left + right)/2
//但是不能用right/2+left/2,因为如果左右都是奇数的话结果不正确
            if (nums[middle] > target) {
                right = middle - 1; // target 在左区间,所以[left, middle - 1]
            } else if (nums[middle] < target) {
                left = middle + 1; // target 在右区间,所以[middle + 1, right]
            } else { // nums[middle] == target
                return middle; // 数组中找到目标值,直接返回下标
            }
        }
        // 未找到目标值
        return -1;
    }
};

35. Search insertion position

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        int left = 0;
        int right = nums.size() - 1;
        while (left <= right) {
            //cout<<left<<endl;
            //cout<<right<<endl;
            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 left;
    }
};

34. Find the first and last position of an element in a sorted array

class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        int left = searchLeft(nums, target);
        int right = searchRight(nums, target);
        vector<int> res(2, -1);
        res[0] = left;
        res[1] = right;
        return res;
    }

    int searchLeft(vector<int>& nums, int target){
        int l = 0, r = nums.size() - 1;
        while(l <= r) {
            int mid = l + (r - l) / 2;
            if (nums[mid] > target){
                r = mid - 1;
            }else if (nums[mid] < target){
                l = mid + 1;
            }else {
                if (mid == 0 || nums[mid - 1] != nums[mid]){
                    return mid;
                }else r = mid - 1;
            }
        }
        return -1;
    }

    int searchRight(vector<int>& nums, int target){
        int l = 0, r = nums.size() - 1;
        while(l <= r) {
            int mid = l + (r - l) / 2;
            if (nums[mid] > target){
                r = mid - 1;
            }else if (nums[mid] < target){
                l = mid + 1;
            }else {
                if (mid == nums.size() - 1 || nums[mid + 1] != nums[mid]){
                    return mid;
                }else l = mid + 1;
            }
        }
        return -1;
    }
};

 69. Square root of x 

class Solution {
public:
    int mySqrt(int x) {
        long l=0,r=x;
        while(l<=r){
            long mid=l+(r-l)/2;
            if(mid*mid==x){
                return mid;
            }else if(mid*mid>x){
                r=mid-1;
            }else{
                l=mid+1;
            }
        }
        return l-1;
    }
};

27. Remove elements

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int slow = 0;
        int fast = 0;
        for(;fast < nums.size(); fast++){//快指针遍历数组
            if(nums[fast]!=val){
                nums[slow++]=nums[fast];
            }
        }
        return slow;
    }
};

 26. Remove duplicates in sorted array

class Solution {
public:
//对比27题,自己定义val
    int removeDuplicates(vector<int>& nums) {
        int slow=0,fast=0;
        int val=10001;
        for(; fast < nums.size();fast++){
            if(fast>0) val=nums[fast-1];
            if(nums[fast]!=val){
                nums[slow++]=nums[fast];
            }
        }
        return slow;
    }
};

Guess you like

Origin blog.csdn.net/hbzdsXCV/article/details/130599774