每日一题(3.13~3.19)

118 阅读2分钟

image.png

题解:模拟
代码:

class Solution {
public:
    bool validUtf8(vector<int>& data) {
        int bit = 0;
        for(auto num : data){
            if(bit == 0){
                if((num >>5) == 0b110) bit = 1;
                else if((num >>4) == 0b1110) bit = 2;
                else if((num >>3) == 0b11110) bit = 3;
                else if(num >>7) return false;
            }else{
                if((num >> 6) != 0b10) return false;
                bit--;
            }
        }
        return bit == 0;
    }
};

image.png

题解:哈希表
代码:

class Solution {
public:
    vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
        map<string, int> s1;
        for(int i=0; i<list1.size(); i++){
            s1[list1[i]] = i;
        }
        map<string,int> s2;
        for(int i=0; i<list2.size(); i++){
            s2[list2[i]] = i;
        }
        set<string> con(list1.begin(), list1.end());
        for(auto& s : list2){
            con.insert(s);
        }
        vector<string> res;
        int minv = INT_MAX;
        for(auto&s : con){
            if(s1.count(s) && s2.count(s)){
                int diff = s1[s] + s2[s];
                if( diff < minv){
                    minv = diff;
                    res.clear();
                    res.push_back(s);
                }else if(minv == diff){
                    res.push_back(s);
                }
            }
        }
        return res;
    }
};

image.png

题解:二进制枚举
代码:

class Solution {
public:
    int countMaxOrSubsets(vector<int>& nums) {
        int n = nums.size(), m = 1<<n;
        int maxv = 0, res = 0;
        for(int i=0; i<m; i++){
            int u = 0;
            for(int j=0; j<n; j++){
                if((i>>j)&1==1){
                    u |= nums[j];
                }
            }
            if(u > maxv){
                maxv = u;
                res = 1;
            }else if(u == maxv){
                res ++;
            }
        }
        return res;
    }
};

image.png

题解: 哈希表
代码:

class AllOne {
public:
   unordered_map<string, int> f;
   set<pair<int, string>> set;
    AllOne() {

    }
    
    void inc(string key) {
        int count = f[key]++;
        if(count){
            set.erase({count, key});
        }
        set.insert(make_pair(f[key], key));
    }
    
    void dec(string key) {
        if(f[key] <= 0){
            return;
        }
        int count = f[key]--;
        set.erase({count, key});
        if(f[key]){
            set.insert({f[key], key});
        }
    }
    
    string getMaxKey() {
        if(set.empty()) return "";
        return set.rbegin()->second;
    }
    
    string getMinKey() {
        if(set.empty()) return "";
        return set.begin()->second;
    }
};

/**
 * Your AllOne object will be instantiated and called as such:
 * AllOne* obj = new AllOne();
 * obj->inc(key);
 * obj->dec(key);
 * string param_3 = obj->getMaxKey();
 * string param_4 = obj->getMinKey();
 */

image.png

题解: 字典树
代码:

const int N = 3e4+10;
const int M = 26;
class Solution {
public:
    int f[N][M];
    bool st[N];
    int idx;
    void add(string s){
        int n = s.size();
        int p = 0;
        for(int i=0; i<n; i++){
            if(f[p][s[i]-'a'] == 0) f[p][s[i]-'a'] = ++idx;
             p = f[p][s[i]-'a'];
        }
        st[p] = true;
    }
    bool query(string s){
        int n = s.size();
        int p = 0;
        for(int i=0; i<n; i++){
            p = f[p][s[i]-'a'];
            if(!st[p]) return false;
        }
        return true;
    }
    string longestWord(vector<string>& words) {
        memset(f, 0, sizeof f);
        memset(st, false, sizeof st);
        idx = 0;
        for(auto& s : words){
            add(s);
        }
        string res;
        for(auto& s : words){
            if(s.size() < res.size()) continue;
            if(s.size() == res.size() && s > res){
                 continue;
            }
            if(query(s)) res = s;
        }
        return res;
    }
};

image.png

题解: 哈希表
代码:

class Bank {
public:
    unordered_map<int, long long> map;
    Bank(vector<long long>& balance) {
        for(int i=1; i<=balance.size(); i++){
            map[i-1] = balance[i-1];
        }
    }
    
    bool transfer(int account1, int account2, long long money) {
        if(account1 <= 0 || account1 > map.size()) return false;
        if(account2 <= 0 || account2 > map.size()) return false;
         if(map[account1-1] >= money){
             map[account1-1] -= money;
             map[account2-1] += money;
             return true;
         }
         return false;
    }
    
    bool deposit(int account, long long money) {
        if(account <= 0 || account > map.size()) return false;
        map[account-1] += money;
        return true;
    }
    
    bool withdraw(int account, long long money) {
        if(account <= 0 || account > map.size()) return false;
        if(map[account-1] < money) return false;
        map[account-1] -= money;
        return true;
    }
};

/**
 * Your Bank object will be instantiated and called as such:
 * Bank* obj = new Bank(balance);
 * bool param_1 = obj->transfer(account1,account2,money);
 * bool param_2 = obj->deposit(account,money);
 * bool param_3 = obj->withdraw(account,money);
 */

image.png

题解: 深度搜索
代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    string dfs(TreeNode* root){
        if(!root) return "";
        else{
            string res = to_string(root->val);
            if(root->left){
                string left = dfs(root->left);
                res += "("+left+")";
            }
            if(!root->left && root->right){
                res += "()";
            }
            if(root->right){
                string right = dfs(root->right);
                res += "("+right+")";
            }
           return res;
        }
    }
    string tree2str(TreeNode* root) {
        return dfs(root);
    }
};