头像

dys

克莱登大学纪委组组长


访客:3794

离线:5小时前


活动打卡代码 LeetCode 205. 同构字符串

dys
8小时前
class Solution {
public:
    bool isIsomorphic(string s, string t) {
        unordered_map<char, char> st, ts;
        if (s.size() != t.size()) return false;
        for (int i = 0; i < s.size(); i ++ )
        {
            if (st.count(s[i]))
            {
                if (st[s[i]] != t[i]) return false;
            }
            else st[s[i]] = t[i];

            if (ts.count(t[i]))
            {
                if (ts[t[i]] != s[i]) return false;
            }
            else ts[t[i]] = s[i];
        }
        return true;
    }
};




dys
9小时前
class Solution {
    public int minSubArrayLen(int s, int[] nums) {
        if(nums.length == 0)    return 0;

        //窗口的左右边缘
        int left = 0;
        int right = 0;

        // 长度最小子数组的左右边沿
        int begin = -1;
        int end = -1;

        //目前窗口内的子数组和
        int cur = nums[0];

        //窗口开始滑动!
        for(left = 0; left < nums.length; left++){
            //如果某一个数字特别大,大于等于s,直接就返回1
            if(nums[left] >= s) return 1;

            while(cur < s){
                //目前的子数组和不够,窗口向右滑动才能让子数组和增大,right++
                if(right == nums.length-1)  break;
                right ++;
                cur += nums[right];
            }

            //更新begin、end的情况
            if(cur>= s && (begin == -1 || (right - left) < (end - begin))){
                begin = left;
                end = right;
            }
            //left向右一位了
            cur -= nums[left];
        }

        if(begin == -1) 
            return 0;
        else    
            return (end-begin+1);
    }
}


活动打卡代码 LeetCode 204. 计数质数

dys
1天前
class Solution {
public:
    int countPrimes(int n) {
        vector<int> primes;
        vector<bool> st(n + 1);
        for (int i = 2; i < n; i ++ )
        {
            if (!st[i]) primes.push_back(i);
            for (int j = 0; i * primes[j] < n; j ++ )
            {
                st[i * primes[j]] = true;
                if (i % primes[j] == 0) break;
            }
        }
        return primes.size();
    }
};

线性筛



活动打卡代码 LeetCode 202. 快乐数

dys
1天前
class Solution {
public:
    bool isHappy(int n) {
        return dfs(n);
    }
    bool dfs(int &x){
        unordered_map<int,int> hash;
        while(1){
            if(x==1) return true;
            hash[get(x)]++;
            x=get(x);
            if(hash[x]==2) return false;
        }
    }
    int get(int x){
        int b=0;
        while(x){
            b+=(x%10)*(x%10);
            x/=10;
        }
        return b;
    }
};


活动打卡代码 AcWing 90. 64位整数乘法

dys
2天前
#include<iostream>
using namespace std;
typedef unsigned long long ULL;
int main(){
    ULL a,b,p;
    cin>>a>>b>>p;
    ULL res=0;
    while(b){
        if(b&1) res=(res+a)%p;
        b>>=1;
        a=(a+a)%p;
    }
    cout<<res;
    return 0;
}


活动打卡代码 LeetCode 198. 打家劫舍

dys
3天前
class Solution {
public:
    int rob(vector<int>& nums) {
        int n=nums.size();
        vector<int>f(n,0);
        int ans=0;
        for(int i=0;i<nums.size();i++){
            f[i]=nums[i];
            int k=0;
            for(int j=0;j+2<=i;j++){
                k=max(k,f[j]);
            }
            if(i>=2) f[i]+=k;
        }
        for(int i=0;i<nums.size();i++) ans=max(ans,f[i]);
        return ans; 
    }
};



dys
3天前
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode *dummy = new ListNode(-1);
        dummy->next = head;
        for (ListNode *p = dummy; p;)
        {
            if (p->next && p->next->val == val)
                p->next = p->next->next;
            else
                p = p->next;
        }
        return dummy->next;
    }
};



dys
3天前
class Solution {
public:
    int rangeBitwiseAnd(int m, int n) {
        int len=0;
        while(m<n){
            m>>=1;
            n>>=1;
            len++;
        }
        return m<<len;
    }
};


活动打卡代码 LeetCode 191. 位1的个数

dys
3天前
class Solution {
public:
    int hammingWeight(uint32_t n) {
        int res=0;
        for(int i=0;i<32;i++){
            res+=(n>>i&1);
        }
        return res;
    }
};



dys
3天前
class Solution {
public:
    uint32_t reverseBits(uint32_t n) {
        uint32_t res=0;
        for(int i=0;i<32;i++){
            res=(res<<1)+(n>>i&1);
        }
        return res;
    }
};