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));
}
};