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
import React, { useState, useEffect } from 'react'; | |
const InfiniteScroll = () => { | |
const [data, setData] = useState([]); | |
const [page, setPage] = useState(1); // Track current page | |
const [loading, setLoading] = useState(false); // Track loading state | |
const fetchData = async () => { | |
setLoading(true); // Set loading state | |
try { |
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
const inorderTraversal=function(root){ | |
let node = root; | |
const result=[] | |
while (node) { | |
if (!node.left) { | |
result.push(node.val); | |
node = node.right; | |
} else { | |
const predecessor = findPredecessor(node); | |
if (predecessor.right === node) { |
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 partition(a, lo, hi) { | |
/* | |
//Improve partition choice for the worst case | |
const randomIndex = Math.floor(Math.random() * (hi - lo + 1)) + lo; | |
swap(a, lo, randomIndex); // Move the random pivot to the start | |
*/ | |
let i = lo; | |
let j = hi + 1; | |
let P = a[lo]; |
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 Twitter { | |
constructor() { | |
this.users = {}; | |
this.tweets = []; | |
this.followers = {}; | |
} | |
postTweet(userId, content) { | |
const tweet = { | |
id: this.tweets.length, |
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 { | |
constructor(val) { | |
this.val = val; | |
this.children = []; | |
} | |
addChild = (child) => this.children.push(child) | |
} | |
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
const getAllKeys = (obj) => { | |
const result = []; | |
const queue = [{ path: [], obj }]; | |
while (queue.length) { | |
const { path, obj: currentObj } = queue.shift(); | |
for (const [key, value] of Object.entries(currentObj)) { | |
const newPath = path.concat(key); | |
if (typeof value === 'object' && value !== null) { | |
queue.push({ path: newPath, obj: value }); |
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
const quickSelect = (nums, k, kthMax) => { | |
if (!nums.length) return; | |
const P = nums[Math.floor(Math.random() * nums.length)]; | |
const left = []; | |
const mid = []; | |
const right = []; | |
nums.forEach(x => { |
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 TrieNode { | |
constructor() { | |
this.children = {} | |
this.eow = false | |
this.word = "" | |
} | |
} | |
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 Node { | |
constructor(val = 0, left = null, right = null) { | |
this.val = val; | |
this.left = left; | |
this.right = right; | |
} | |
} | |
const root = new Node(6) | |
root.left = new Node(3) |
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
//https://pokeapi.co/api/v2/pokemon?limit=100 | |
//https://pokeapi.co/api/v2/gender/1 | |
//https://pokeapi.co/api/v2/pokemon/1000 | |
const fetcher = async (url) => { | |
try { | |
const res = await fetch(url) | |
if (res.ok || res.status === 200) return await res.json() | |
} | |
catch (e) { |
NewerOlder