Skip to content

Instantly share code, notes, and snippets.

class Solution {
public:
int rangeBitwiseAnd(int m, int n) {
int res = m&n;
int diff = log2(n-m);
int tmp = 1;
for(int i=0; i<=diff; i++)
res = res&((~tmp)<<i);
return res;
}
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
int res=INT_MAX;
if(nums.empty())
return 0;
int p0=0, p1=0;
int sum = 0;
for(; p1<=nums.size(); ){
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
TreeNode* res=NULL;
if(root==NULL)
return res;
if(p->val>q->val)
return lowestCommonAncestor(root, q, p);
/**
* 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 {
class Solution {
public:
void buildGraph(vector<string>& words, vector<int>& charsAll, vector<set<char> >& pntTo, vector<set<char> >& pntBy){
for(int i=0; i<words.size(); i++){
string word1=words[i];
for(int j=0; j<word1.length(); j++)
charsAll[word1[j]-'a']=1;
}
for(int i=0; i<words.size()-1; i++){
class Solution {
public:
int missingNumber(vector<int>& nums) {
int result = nums.size();
int i=0;
for(int num:nums){
result ^= num;
result ^= i;
i++;
class Solution {
public:
void BuildGraph(const vector<string>& words, unordered_map<char, unordered_set<int> >& pointTo, unordered_map<char, unordered_set<int> >& pointBy){
for(int i=0; i<words.size()-1; i++){
string w0 = words[i];
string w1 = words[i+1];
for(int j=0; j<min(w0.length(), w1.length()); j++){
if(w0[j]!=w1[j]){
// Forward declaration of isBadVersion API.
bool isBadVersion(int version);
class Solution {
public:
int firstBadVersion(int n) {
int l = 1, r=n;
while(l<r-1){
int m = l+(r-l)/2;
bool tmp = isBadVersion(m);
class Solution {
public:
int numSquares(int n) {
vector<int> myT(n+1, INT_MAX);
myT[0]=0;
for(int i=1; i<=n; i++){
for(int j=1; j*j<=i; j++)
myT[i] = min(myT[i], myT[i-j*j]+1);
}
void wiggleSort(vector<int>& nums) {
if (nums.empty()) {
return;
}
int n = nums.size();
// Step 1: Find the median
vector<int>::iterator nth = next(nums.begin(), n / 2);
nth_element(nums.begin(), nth, nums.end());
int median = *nth;