从零开始的刷LeetCode生活 第49期 521-537

在这里插入图片描述

class Solution {
public:
    int findLUSlength(string a, string b) {
        if(a == b)
            return -1;
        return max(a.size(), b.size());
    }
};

在这里插入图片描述
在这里插入图片描述

class Solution {
public:
    int findLUSlength(vector<string>& strs) {
        unordered_map<string, int>m;
        for(string s : strs)
            for(int i = 0; i < (1 << s.size()); i ++) //生成 2 ^ s.size()个序列
            {
                string t = "";
                for(int j = 0; j < s.size(); j ++)
                    if((i >> j) & 1 != 0)
                        t += s[j];
                if(m[t] != 0)
                    m[t] ++;
                else
                    m[t] = 1;
            }
        int res = -1;
        // for(auto s : m)
        //     cout << s.first << ' ' << s.second << endl;

        for(auto s : m)
            if(s.second == 1)
                res = max(res, int(s.first.size()));
        return res;
    }
};
class Solution {
public:
    int findLUSlength(vector<string>& strs) {
        int res = -1;
        for(int i = 0, j; i < strs.size(); i ++)
        {
            for(j = 0; j < strs.size(); j ++)
            {
                if(i == j) continue;
                if(isSubsequence(strs[i], strs[j]))
                    break;
            }
            if(j == strs.size())
                res = max(res, (int)strs[i].size());
        }
        return res;
    }

    bool isSubsequence(string x, string y)
    {
        int j = 0;
        for(int i = 0; i < y.size() && j < x.size(); i ++)
            if(y[i] == x[j])
                j ++;
        return j == x.size();
    }
};

在这里插入图片描述

class Solution {
public:
    bool checkSubarraySum(vector<int>& nums, int k) {        
        int tol = 0;
        for(int i = 0; i < nums.size(); i ++)
        {
            tol = nums[i];
            for(int j = i + 1; j < nums.size(); j ++)
            {
                tol += nums[j];
                if(k != 0)
                {
                    if(tol % k == 0)
                        return true;
                }
                else if(k == 0 && tol == 0) //else if 跟最近的if
                    return true;
            }
        }
        return false;
    }
};

在这里插入图片描述

class Solution {
public:
    string findLongestWord(string s, vector<string>& d) {
        vector<string>res;
        for(int i = 0; i < d.size(); i ++)
        {
            if(isSubsequence(d[i], s))
                res.push_back(d[i]);
        }
        sort(res.begin(), res.end(), [](auto a, auto b)
        {
            return a.size() > b.size() || a.size() == b.size() && a < b;
        });
        return res.size() > 0? res[0] : "";
        
    }

    bool isSubsequence(string x, string y)
    {
        int j = 0;
        for(int i = 0; i < y.size() && j < x.size(); i ++)
            if(y[i] == x[j])
                j ++;
        return j == x.size();
    }
};

在这里插入图片描述

class Solution {
public:
    int findMaxLength(vector<int>& nums) {
        unordered_map<int, int>hash;
        int res = 0, sum = 0;
        hash[0] = -1; //前缀和0 下标-1
        for(int i = 0; i < nums.size(); i ++)
        {
            sum += nums[i] ? 1 : -1;
            if(hash.count(sum))
                res = max(res, i - hash[sum]);
            else
                hash[sum] = i;
        }
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    int res = 0;
    int countArrangement(int N) {
        vector<bool> flag(N +1);
        dfs(flag, N, 1);
        return res;
    }

    void dfs(vector<bool>&flag, int N, int count)
    {
        if(count == N + 1)
        {
            res ++;
            return;
        }
        for(int i = 1; i <= N; i ++)
        {
            if(flag[i]) continue;
            if(i % count != 0 && count % i != 0) continue;
            flag[i] = true;
            dfs(flag, N, count + 1);
            flag[i] = false;
        }
    }
};

在这里插入图片描述

class Solution {
public:
    vector<int>s;
    int l, r;
    Solution(vector<int>& w) {
        s = w;
        for(int i = 1; i < s.size(); ++i)
            s[i] += s[i - 1];
        l = 0;
        r = s.back();
    }
    
    int pickIndex() {
        //if(s.size() == 1) return 0;
        int rr = rand() % (r - l) + 1;
        return lower_bound(s.begin(), s.end(), rr) - s.begin();
    }
};

/**
 * Your Solution object will be instantiated and called as such:
 * Solution* obj = new Solution(w);
 * int param_1 = obj->pickIndex();
 */

在这里插入图片描述

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int getMinimumDifference(TreeNode* root) {
        int res = INT_MAX;
        int pre = INT_MAX;
        dfs(root, res, pre);
        return res;
    }

    void dfs(TreeNode* root, int &res, int &pre)
    {
        if(root == NULL)return;
        dfs(root->left, res, pre);
        int tmp = abs(pre - root->val);
        res = min(res, tmp);
        pre = root->val;
        dfs(root->right, res, pre);
        return;
    }
};

在这里插入图片描述

class Solution {
public:
    int findPairs(vector<int>& nums, int k) {
        int res = 0;
        unordered_map<int, int>hash;
        for(auto x : nums)
            hash[x] ++;
        for(auto x : hash)
        {
            if(k == 0 && x.second > 1) res ++;
            if(k > 0 && hash.count(x.first + k)) res ++;
        }
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    int key = 520;
    // Encodes a URL to a shortened URL.
    string encode(string longUrl) {
        string tmp = longUrl;
        for(int i = 0; i < tmp.size(); i ++)
            tmp[i] ^= key;
        return tmp;
    }

    // Decodes a shortened URL to its original URL.
    string decode(string shortUrl) {
        return encode(shortUrl);
    }
};

// Your Solution object will be instantiated and called as such:
// Solution solution;
// solution.decode(solution.encode(url));

在这里插入图片描述
在这里插入图片描述

class Solution {
public:
    string complexNumberMultiply(string a, string b) {
        int a1, b1, a2, b2;
        sscanf(a.c_str(), "%d+%di", &a1, &b1);
        sscanf(b.c_str(), "%d+%di", &a2, &b2);

        int aa = a1 * a2 - b1 * b2;
        int bb = a1 * b2 + a2 * b1;

        return to_string(aa) + "+" + to_string(bb) + "i";
    }
};
class Solution {
public:
    string complexNumberMultiply(string a, string b) {
        int plusA = a.find("+");
        int plusB = b.find("+");
        int a1, b1, a2, b2;
        stringstream ssa(a.substr(0, plusA));
        ssa >> a1;
        stringstream ssb(a.substr(plusA + 1));
        ssb >> b1;
        stringstream ssc(b.substr(0, plusB));
        ssc >> a2;
        stringstream ssd(b.substr(plusB + 1));
        ssd >> b2;

        int x = a1 * a2 - b1 * b2;
        int y = a1 * b2 + a2 * b1;
        return to_string(x) + "+" + to_string(y) + "i";

    }
};

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值