从零开始的刷LeetCode生活 第37期 381-390

在这里插入图片描述

class RandomizedCollection {
public:
    /** Initialize your data structure here. */
    unordered_map<int, unordered_set<int>>hash;
    vector<int>nums;
    RandomizedCollection() {
        
    }
    
    /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
    bool insert(int val) {
        bool res = hash.find(val) == hash.end();
        hash[val].insert(nums.size());
        nums.push_back(val);
        return res;
    }
    
    /** Removes a value from the collection. Returns true if the collection contained the specified element. */
    bool remove(int val) {
        if(hash.find(val) != hash.end())
        {
            if(nums.back() == val)
            {
                nums.pop_back();
                hash[val].erase(nums.size());
                if(hash[val].empty())
                    hash.erase(val);
                return true;
            }
            int t = *hash[val].begin();
            hash[nums.back()].erase(nums.size() - 1);
            hash[nums.back()].insert(t);

            swap(nums[t], nums.back());

            nums.pop_back();
            hash[val].erase(t);
            if(hash[val].empty())
                hash.erase(val);
            return true;
        }
        return false;
    }
    
    /** Get a random element from the collection. */
    int getRandom() {
        return nums[rand() % nums.size()];
    }
};

/**
 * Your RandomizedCollection object will be instantiated and called as such:
 * RandomizedCollection* obj = new RandomizedCollection();
 * bool param_1 = obj->insert(val);
 * bool param_2 = obj->remove(val);
 * int param_3 = obj->getRandom();
 */

在这里插入图片描述

/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
   /** @param head The linked list's head.
       Note that the head is guaranteed to be not null, so it contains at least one node. */
   Solution(ListNode* head) : head(head) {
       
   }
   
   /** Returns a random node's value. */
   int getRandom() {
       int i = 2;
       ListNode *cur = head->next;
       int val = head->val;
       while(cur != nullptr)
       {
           if(rand() % i + 1 == 1) val = cur->val;
           i ++;
           cur = cur->next;
       }
       return val;
   }
private:
   ListNode *head;
};

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

在这里插入图片描述

class Solution {
public:
   bool canConstruct(string ransomNote, string magazine) {
       int hash[26] = {0};
       for(auto c : magazine)
           hash[c - 'a'] ++;
       for(auto c : ransomNote)
       {
           hash[c - 'a'] --;
           if(hash[c - 'a'] < 0) return false;
       }
       return true;        
   }
};

在这里插入图片描述

class Solution {
public:
    Solution(vector<int>& nums) {
        temp = nums;
    }
    
    /** Resets the array to its original configuration and return it. */
    vector<int> reset() {
        return temp;
    }
    
    /** Returns a random shuffling of the array. */
    vector<int> shuffle() {
        vector<int> tShuffle = temp;
        for(int i = 1; i < tShuffle.size(); i ++)
        {
            int r = rand() % (i + 1);
            if(r != i) swap(tShuffle[r], tShuffle[i]);
        }
        return tShuffle;
    }
private:
    vector<int>temp;
};

/**
 * Your Solution object will be instantiated and called as such:
 * Solution* obj = new Solution(nums);
 * vector<int> param_1 = obj->reset();
 * vector<int> param_2 = obj->shuffle();
 */

在这里插入图片描述

/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* class NestedInteger {
*   public:
*     // Constructor initializes an empty nested list.
*     NestedInteger();
*
*     // Constructor initializes a single integer.
*     NestedInteger(int value);
*
*     // Return true if this NestedInteger holds a single integer, rather than a nested list.
*     bool isInteger() const;
*
*     // Return the single integer that this NestedInteger holds, if it holds a single integer
*     // The result is undefined if this NestedInteger holds a nested list
*     int getInteger() const;
*
*     // Set this NestedInteger to hold a single integer.
*     void setInteger(int value);
*
*     // Set this NestedInteger to hold a nested list and adds a nested integer to it.
*     void add(const NestedInteger &ni);
*
*     // Return the nested list that this NestedInteger holds, if it holds a nested list
*     // The result is undefined if this NestedInteger holds a single integer
*     const vector<NestedInteger> &getList() const;
* };
*/
class Solution {
public:
   NestedInteger deserialize(string s) {
       if(s.empty()) return NestedInteger();
       if(s[0] != '[') return NestedInteger(stoi(s));
       if(s.size() <= 2) return NestedInteger();
       NestedInteger res;
       int start = 1, cnt = 0;
       for(int i = 1; i < s.size(); i ++)
       {
           if(cnt == 0 && (s[i] == ',' || i == s.size() - 1))
           {
               res.add(deserialize(s.substr(start, i - start)));
               start = i + 1;
           }
           else if(s[i] == '[') cnt ++;
           else if(s[i] == ']') cnt --;
       }
       return res;
   }
};

在这里插入图片描述

class Solution {
public:
    vector<int> res;
    vector<int> lexicalOrder(int n) {
        for(int i = 1; i < 10; i ++)
            dfs(i, n);
        return res;
    }

    void dfs(int t, int n)
    {
        if(t > n)
            return;
        res.push_back(t);
        for(int i = 0; i < 10; i ++)
            dfs(t * 10 + i, n);
    }
};

在这里插入图片描述

class Solution {
public:
    int firstUniqChar(string s) {
        int hash[26] = {0};
        for(int i = 0; i < s.size(); i ++)
        {
            hash[s[i] - 'a'] ++;            
        }
        for(int i = 0; i < s.size(); i ++)
            if(hash[s[i] - 'a'] == 1)
                return i;
        return -1;                
    }
};

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

class Solution {
public:
    string s;
    int level[100]; //各层长度
    int index, cnt, i, len, max_len, tmp;
    int lengthLongestPath(string input) {
        input.push_back('\n');
        len = input.size();
        while(i < len)
        {
            index = input.find('\n', i);
            s = input.substr(i, index - i);
            level[cnt] = s.size() + 1;
            if(judge(s))
            {
                tmp = 0;
                for(int i = 0; i <= cnt; i ++) tmp += level[i];
                max_len = max(max_len, tmp - 1);
            }
            cnt = 0;
            i = index + 1;
            while(i < len && input[i] == '\t') cnt ++, i ++;
        }
        return max_len;
    }
    bool judge(string a)
    {
        int index;
        index = s.find_last_of('.', s.size() - 1);
        if(index == -1 || index == s.size() - 1) return false;
        else return true;  
    }
};

在这里插入图片描述

class Solution {
public:
    char findTheDifference(string s, string t) {
        int res = 0;
        for(auto c : t)
            res += c;
        for(auto c : s)
            res -= c;
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    int lastRemaining(int n) {
        return n == 1 ? 1 : 2 * (n / 2 + 1 - lastRemaining(n / 2));
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值