Data structure basics day1

Topic: 136. Numbers that appear only once

Requires time linear complexity and space constant complexity

Solution: Bitwise XOR

Regarding the properties of XOR: 1. XOR with 0 is equal to itself; 2. XOR with itself is equal to 0; 3. XOR operation has commutative law and associative law

class Solution {
    
    
public:
    int singleNumber(vector<int>& nums) {
    
    
        int t=0;
        for(int num:nums) t = t^num;	//(a⊕a)⊕(a⊕a)⊕⋯⊕(a⊕a)⊕t
        return t;
    }
};

Topic: 169. Majority elements

My solution: (wrong)

The vector is not initialized and the complexity is very high. Pay attention to the difference between using vector and map. Map does not need to initialize the size.

class Solution {
    
    
public:
    int majorityElement(vector<int>& nums) {
    
    
        int n = nums.size(),ans;
        vector<int> counts;
        for(int num:nums){
    
    
            counts[num]++;
        }
        for(int i=0; i<counts.size(); ++i){
    
    
            if(counts[i]>n/2){
    
    
                ans = i;
                break;
            }
        }
        return ans;
    }
};

Other solution 1: Hash table

class Solution {
    
    
public:
    int majorityElement(vector<int>& nums) {
    
    
        int ans, cnt=0;
        unordered_map<int, int> hash;	//使用hash表存储每个数字出现的次数,取出次数最多的数字
        for(int num:nums){
    
    
            ++hash[num];
            if(hash[num]>cnt){
    
    	//在循环中直接记录最大值
                ans = num;
                cnt = hash[num];
            }
        }
        return ans;
    }
};

Other solution 2: Sorting

After sorting, the mode will appear at position n/2

class Solution {
    
    
public:
    int majorityElement(vector<int>& nums) {
    
    
        sort(nums.begin(), nums.end());	//注意sort的写法,头和尾
        return nums[nums.size()/2];
    }
};

Other solution 3: "Occupy the top of the mountain"

Set a candidate mode candidate and the number of times it appears cnt (initially 0).
If we record the mode as +1 and other numbers as −1, and add them all up, it is obvious that the sum is greater than 0. From the result itself, we It can be seen that there are more modes than other numbers.

class Solution {
    
    
public:
    int majorityElement(vector<int>& nums) {
    
    
        int candidate, cnt=0;
        for(int num:nums){
    
    
            if(cnt==0){
    
    	//如果目前没有候选者,当前元素成为候选者
                candidate=num;
            }
            if(candidate==num){
    
    	//是候选者,数量++
                ++cnt;
            }else{
    
    	//不是候选者,数量--,直到等于0,设立新的候选者,众数数量最多,最后一定会是候选者
                --cnt;
            }
        }
        return candidate;
    }
};

Topic: 15. Sum of three numbers

Solution: sort ➕ double pointer

class Solution {
    
    
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
    
    
        int n = nums.size();
        if(n<3) return {
    
    };	//特判,元素个数小于3,返回空数组
        sort(nums.begin(), nums.end());	//排序
        vector<vector<int>> ans;	//存储答案
        for(int i=0; i<n; ++i){
    
    	//从头遍历
            if(nums[i]>0) return ans;	//如果目前元素为最小元素都大于0,则不可能存在这之后有三个元素加和等于0
            if(i>0 && nums[i]==nums[i-1]) continue;	//元素不能重复
            int k = -nums[i];	//求两数之和等于-nums[i]
            int t = n-1;		//尾指针
            int h = i+1;	//头指针
            while(h<t){
    
    	//外条件:头小于尾
                if(nums[h]+nums[t]>k){
    
    	//如果两数之和大于k,则尾指针前移
                    --t;
                }else if(nums[h]+nums[t]==k){
    
    	//若等于k,则存储这三个数,同时移动头尾指针
                    ans.push_back({
    
    nums[i], nums[h], nums[t]});
                    --t; ++h;
                    while(h<t && nums[h]==nums[h-1]) ++h;	//头指针不重复
                    while(h<t && nums[t]==nums[t+1]) --t;	//尾指针不重复
                }else{
    
    	//若小于k,头指针后移
                    ++h;
                }
            }
        }
        return ans;	//返回答案
    }
};

Guess you like

Origin blog.csdn.net/qq_43606119/article/details/130207502