class Solution {
public:
bool res = true;
bool isValidSerialization(string preorder) {
preorder += ',';
int u = 0;
dfs(preorder, u);
return res && u == preorder.size();
}
void dfs(string &pre, int &u)
{
if(u == pre.size())
{
res = false;
return;
}
if(pre[u] == '#')
{
u += 2;
return;
}
while(pre[u] != ',') u ++;
u ++;
dfs(pre, u);
dfs(pre, u);
}
};
class Solution {
public:
unordered_map<string, multiset<string>>g;
vector<string> res;
vector<string> findItinerary(vector<vector<string>>& tickets) {
for(auto &ticket : tickets) g[ticket[0]].insert(ticket[1]);
dfs("JFK");
return vector<string>(res.rbegin(), res.rend());
}
void dfs(string ver)
{
while(g[ver].size())
{
string next = *g[ver].begin();
g[ver].erase(g[ver].begin());
dfs(next);
}
res.push_back(ver);
}
};
class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
int min_a = INT_MAX, min_b = INT_MAX;
for(auto x : nums)
{
if(x <= min_a)
min_a = x;
else if(x <= min_b)
min_b = x;
else
return true;
}
return false;
}
};
class Solution {
public:
bool isSelfCrossing(vector<int>& x) {
int n = x.size();
if(n <= 3) return false;
for(int i = 3; i < n; i ++)
{
if(x[i - 3] >= x[i - 1] && x[i - 2] <= x[i]) return true;
if(i >= 4 && x[i - 1] == x[i - 3] && x[i - 4] + x[i] >= x[i - 2]) return true;
if(i >= 5 && x[i] + x[i - 4] >= x[i - 2] && x[i - 5] + x[i - 1] >= x[i - 3] && x[i - 1] <= x[i - 3] && x[i - 4] <= x[i - 2]) return true;
}
return false;
}
};
class Solution {
public:
unordered_map<string, int>S;
vector<vector<int>> palindromePairs(vector<string>& words) {
for(int i = 0; i < words.size(); i ++)
{
string key = words[i];
reverse(key.begin(), key.end());
S[key] = i;
}
vector<vector<int>> res;
if(S.count(""))
{
for(int i = 0; i < words.size(); i ++)
if(words[i] != "" && is_palindrome(words[i]))
res.push_back({S[""], i});
}
for(int i = 0; i < words.size(); i ++)
for(int j = 0; j < words[i].size(); j ++)
{
string left = words[i].substr(0, j);
string right = words[i].substr(j);
if(S.count(left) && is_palindrome(right) && S[left] != i) res.push_back({i, S[left]});
if(S.count(right) && is_palindrome(left) && S[right] != i) res.push_back({S[right], i});
}
return res;
}
bool is_palindrome(string &word)
{
for(int i = 0, j = word.size() - 1; i < j; i ++, j --)
if(word[i] != word[j])
return false;
return true;
}
};
/**
* 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:
unordered_map<TreeNode*, unordered_map<int, int>>f;
int rob(TreeNode* root) {
dfs(root);
return max(f[root][0], f[root][1]);
}
void dfs(TreeNode *root)
{
if(!root) return;
dfs(root->left);
dfs(root->right);
f[root][1] = root->val + f[root->left][0] + f[root->right][0]; //在root 拿
f[root][0] = max(f[root->left][0], f[root->left][1]) + max(f[root->right][0], f[root->right][1]);
}
};
class Solution {
public:
vector<int> countBits(int num) {
vector<int>f(num + 1);
f[0] = 0;
for(int i = 1; i <= num; i ++)
f[i] = f[i >> 1] + (i & 1);
return f;
}
};