Skip to content

Instantly share code, notes, and snippets.

/**
* @param {number[]} nums
* @return {number}
*/
var majorityElement = function(nums) {
let occurrences = 1;
let majorityNumber = nums[0];
for (let i = 1; i < nums.length; i++) {
if (nums[i] === majorityNumber) {
var hammingWeight = function(n) {
let bitsCount = 0;
for (let i = 1; i <= 32; i++) {
if (n & 1) {
bitsCount++;
}
n = n >> 1;
}
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
var MinStack = function() {
this.stack = []
this.historyStack = [];
};
/**
* @param {number} val
* @return {void}
*/
MinStack.prototype.push = function(val) {
@esase
esase / swap_two_vars.js
Created April 15, 2022 04:13
Swap two variables using XOR
let a = 2;
let b = 4;
a = a ^ b;
b = a ^ b;
a = a ^ b;
console.log(a, b);
/**
* @param {number[]} nums
* @return {number}
*/
var singleNumber = function(nums) {
let result = 0;
for (let value of nums) {
result ^= value;
}
/**
* @param {string} s
* @return {boolean}
*/
var isPalindrome = function(s) {
s = s.replace(/[^a-z0-9/s]/gi, '').toLowerCase();
return s.split('').reverse().join('') === s;
};
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} p
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root