class Solution {
public:
int rangeBitwiseAnd(int m, int n) {
int i = 0;
while(m != n)
{
m >>= 1;
n >>= 1;
i ++;
}
return n << i;
}
};
class Solution {
public:
int bitSquareSum(int n)
{
int sum = 0;
while(n > 0)
{
int bit = n % 10;
sum += bit * bit;
n /= 10;
}
return sum;
}
bool isHappy(int n) {
int slow = n, fast = n;
do{
slow = bitSquareSum(slow);
fast = bitSquareSum(fast);
fast = bitSquareSum(fast);
}while(slow != fast);
return slow == 1;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode *dummy = new ListNode(-1);
dummy->next = head;
for(ListNode *p = dummy; p; )
{
if(p->next && p->next->val == val)
p->next = p->next->next;
else
p = p->next;
}
return dummy->next;
}
};
class Solution {
public:
int countPrimes(int n) {
vector<int> primes;
vector<bool> st(n + 1);
for(int i = 2; i < n; i ++)
{
if(!st[i]) primes.push_back(i);
for(int j = 0; i * primes[j] < n; j ++)
{
st[i * primes[j]] = true;
if(i % primes[j] == 0) break;
}
}
return primes.size();
}
};
class Solution {
public:
bool isIsomorphic(string s, string t) {
unordered_map<char, char>st, ts;
for(int i = 0; i < s.size(); i ++)
{
if(st.count(s[i]))
{
if(st[s[i]] != t[i]) return false;
}
else st[s[i]] = t[i];
if(ts.count(t[i]))
{
if(ts[t[i]] != s[i]) return false;
}
else ts[t[i]] = s[i];
}
return true;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(!head || !head->next) return head;
ListNode *p = reverseList(head->next);
head->next->next = head;
head->next = NULL;
return p;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(!head) return head;
ListNode *p = NULL;
while(head)
{
ListNode *next = head->next;
head->next = p;
p = head;
head = next;
}
return p;
}
};
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<vector<int>> graph(numCourses);
vector<int> in_degree(numCourses, 0);
for(int i = 0; i < prerequisites.size(); i ++)
{
in_degree[prerequisites[i][0]] ++;
graph[prerequisites[i][1]].push_back(prerequisites[i][0]);
}
queue<int> q;
vector<bool> vis(numCourses, false);
for(int i = 0; i < numCourses; i ++)
if(in_degree[i] == 0)
q.push(i);
while(!q.empty())
{
int st = q.front();
q.pop();
vis[st] = true;
for(int i = 0; i < graph[st].size(); i ++)
{
in_degree[graph[st][i]] --;
if(in_degree[graph[st][i]] == 0)
q.push(graph[st][i]);
}
}
for(int i = 0; i < numCourses; i ++)
if(vis[i] == false)
return false;
return true;
}
};
class Trie {
public:
/** Initialize your data structure here. */
struct Node
{
bool is_end;
Node *next[26];
Node()
{
is_end = false;
for(int i = 0; i < 26; i ++)
next[i] = 0;
}
}*root;
Trie() {
root = new Node();
}
/** Inserts a word into the trie. */
void insert(string word) {
Node *p = root;
for(char c : word)
{
int son = c - 'a';
if(!p->next[son]) p->next[son] = new Node();
p = p->next[son];
}
p->is_end = true;
}
/** Returns if the word is in the trie. */
bool search(string word) {
Node *p = root;
for(char c : word)
{
if(p->next[c - 'a']) p = p->next[c - 'a'];
else return false;
}
return p->is_end;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
Node *p = root;
for(char c : prefix)
{
if(p->next[c - 'a']) p = p->next[c - 'a'];
else return false;
}
return true;
}
};
/**
* Your Trie object will be instantiated and called as such:
* Trie* obj = new Trie();
* obj->insert(word);
* bool param_2 = obj->search(word);
* bool param_3 = obj->startsWith(prefix);
*/
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
int n = nums.size();
int start = 0, end = 0, tot = 0, res = n + 1;
while(end < n)
{
tot += nums[end];
while(tot >= s)
{
res = min(res, end - start + 1);
tot -= nums[start];
start ++;
}
end ++;
}
if(res == n + 1)
res = 0;
return res;
}
};
class Solution {
public:
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
vector<int>res;
vector<vector<int>>graph(numCourses);
vector<int> in_degree(numCourses, 0);
for(int i = 0; i < prerequisites.size(); i ++)
{
in_degree[prerequisites[i][0]] ++;
graph[prerequisites[i][1]].push_back(prerequisites[i][0]);
}
queue<int>q;
vector<bool>vis(numCourses, false);
for(int i = 0; i < numCourses; i ++)
if(in_degree[i] == 0)
q.push(i);
while(!q.empty())
{
int st = q.front();
q.pop();
res.push_back(st);
vis[st] = true;
for(int i = 0; i < graph[st].size(); i ++)
{
in_degree[graph[st][i]] --;
if(in_degree[graph[st][i]] == 0)
q.push(graph[st][i]);
}
}
for(int i = 0; i < numCourses; i ++)
if(vis[i] == false)
return vector<int>();
return res;
}
};