class Solution {
public:
int rangeBitwiseAnd(int m, int n) {
if(m == n) return m;
for(int i = 30; i >= 0; i--)
{
if(!(m >> i & 1) && (n >> i & 1))
{
return m >> i << i;
}
}
return 0;
}
};
class Solution {
public:
static const int N = 100010;
vector<int> h, e, ne, din;
int n, idx = 0;
public:
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx++, din[b]++;
}
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
n = numCourses;
h = vector<int>(n, -1), e = vector<int>(n * n), ne = vector<int>(n * n), din = vector<int>(n, 0);
for(auto& edge : prerequisites)
add(edge[0], edge[1]);
int cnt = 0;
// 初始化
queue<int> q;
for(int i = 0; i < n; i++)
if(!din[i]) q.push(i), cnt++;
while(q.size())
{
int t = q.front();
q.pop();
for(int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
din[j]--;
if(din[j] == 0)
q.push(j), cnt++;
}
}
return cnt == n;
}
};
class Trie {
public:
/** Initialize your data structure here. */
struct Node
{
Node* son[26];
bool is_end;
Node()
{
is_end = false;
for(int i = 0; i < 26; i++)
son[i] = nullptr;
}
}*root;
Trie() {
root = new Node();
}
/** Inserts a word into the trie. */
void insert(string word) {
auto p = root;
for(auto c : word)
{
int u = c - 'a';
if(p->son[u] == nullptr) p->son[u] = new Node();
p = p->son[u];
}
p->is_end = true;
}
/** Returns if the word is in the trie. */
bool search(string word) {
auto p = root;
for(auto c : word)
{
int u = c - 'a';
if(p->son[u] == nullptr) return false;
p = p->son[u];
}
return p->is_end;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
auto p = root;
for(auto c : prefix)
{
int u = c - 'a';
if(p->son[u] == nullptr) return false;
p = p->son[u];
}
return true;
}
};
class Solution {
public:
string minWindow(string s, string t) {
int res = 1e6, suc = 0, cnt = 0, ri = 0;
unordered_map<char, int> hash, window;
for(auto c : t)
hash[c]++, cnt++;
for(int l = 0, r = 0; r < s.size(); r++)
{
// 加入滑动窗口
window[s[r]]++;
if(window[s[r]] <= hash[s[r]]) suc++;
// 从滑动窗口中弹出 注意这个条件
while(window[s[l]] > hash[s[l]]) window[s[l++]]--;
if(suc == cnt)
{
if(res > r - l + 1)
{
res = r - l + 1;
ri = l;
}
}
}
if(res == 1e6) return "";
return s.substr(ri, res);
}
};
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
// 时间复杂度要求为o(n)
// 一看就是双指针
int res = 2e9, sum = 0;
for(int l = 0, r = 0; r < nums.size(); r++)
{
// 加入滑动窗口
sum += nums[r];
// 弹出滑动窗口
while(sum >= s)
{
res = min(r - l + 1, res);
sum -= nums[l++];
}
}
if(res == 2e9) return 0;
else return res;
}
};
class Solution {
public:
int n;
vector<int> h, e, ne, din;
int idx = 0;
public:
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx++, din[b]++;
}
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
n = numCourses;
h = vector<int>(n, -1);
e = vector<int>(n * n);
ne = vector<int>(n * n);
din = vector<int>(n);
for(vector<int>& edge : prerequisites)
add(edge[1], edge[0]);
vector<int> q(n);
int hh = 0, tt = -1;
for(int i = 0; i < n; i++)
if(!din[i]) q[++tt] = i;
while(hh <= tt)
{
int t = q[hh++];
for(int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
din[j]--;
if(din[j] == 0) q[++tt] = j;
}
}
if(tt == n - 1) return q;
else return {};
}
};