This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let Stack = (()=>{ | |
let map = new WeakMap(); | |
let _items = []; | |
class Stack { | |
constructor(...items){ | |
// let's store our items array inside the weakmap | |
map.set(this, []); | |
// let's get the items array from map, and store it in _items for easy access elsewhere |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Queue = (()=>{ | |
const map = new WeakMap(); | |
let _items = [] | |
class Queue{ | |
constructor(...items){ | |
//initialize the items in queue | |
map.set(this, []); | |
_items = map.get(this); | |
// enqueuing the items passed to the constructor | |
this.enqueue(...items) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process.stdin.resume(); | |
process.stdin.setEncoding("utf-8"); | |
var stdin_input = ""; | |
process.stdin.on("data", function (input) { | |
stdin_input += input; // Reading input from STDIN | |
}); | |
process.stdin.on("end", function () { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
numArray.sort((a, d) => a - d); // For ascending sort | |
numArray.sort((a, d) => d - a); // For descending sort |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reverseLinkedList(head){ | |
let prev = null; | |
let next = null; | |
while(head){ | |
next = head.next; | |
head.next = prev; | |
prev = head; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getListMiddle(head){ | |
let slow = head; | |
let fast = head; | |
let moveBoth = false; | |
// to get second element, in case of two mid-nodes, use 'fast' instead of 'fast.next' | |
while(fast.next){ | |
if(moveBoth){ | |
slow = slow.next; | |
fast = fast.next; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node: | |
def __init__(self, value): | |
self.value = value | |
self.rank = 1 | |
self.parent = self | |
class DisjointSet: | |
def __init__(self): | |
self.mapping = {} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node: | |
def __init__(self, val=None): | |
self.val = val | |
self.children = {} | |
self.count = 0. # number of times words with this prefix | |
self.end = 0 # number of times this word has been inserted | |
class Trie: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def sortColors(self, nums: List[int]) -> None: | |
""" | |
Do not return anything, modify nums in-place instead. | |
""" | |
start, end = 0, len(nums)-1 | |
i = start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sum(x, y){ | |
if(y === 0) return x | |
return sum( x^y, (x&y)<<1 ) | |
} | |
function multiply(x, y){ | |
if(x ===0 || y ===0) return 0 |
OlderNewer