• Sep 13, 2023 •C S
0 likes • 2 views
/** * @param {number[]} nums * @param {number} target * @return {number[]} */ var twoSum = function(nums, target) { for(let i = 0; i < nums.length; i++) { for(let j = 0; j < nums.length; j++) { if(nums[i] + nums[j] === target && i !== j) { return [i, j] } } } };
• Nov 19, 2022 •CodeCatch
const longestPalindrome = (s) => { if(s.length === 1) return s const odd = longestOddPalindrome(s) const even = longestEvenPalindrome(s) if(odd.length >= even.length) return odd if(even.length > odd.length) return even }; const longestOddPalindrome = (s) => { let longest = 0 let longestSubStr = "" for(let i = 1; i < s.length; i++) { let currentLongest = 1 let currentLongestSubStr = "" let left = i - 1 let right = i + 1 while(left >= 0 && right < s.length) { const leftLetter = s[left] const rightLetter = s[right] left-- right++ if(leftLetter === rightLetter) { currentLongest += 2 currentLongestSubStr = s.slice(left+1, right) } else break } if(currentLongest > longest) { if(currentLongestSubStr) longestSubStr = currentLongestSubStr else longestSubStr = s[i] longest = currentLongest } } return longestSubStr } const longestEvenPalindrome = (s) => { let longest = 0 let longestSubStr = "" for(let i = 0; i < s.length - 1; i++) { if(s[i] !== s[i+1]) continue; let currentLongest = 2 let currentLongestSubStr = "" let left = i - 1 let right = i + 2 while(left >= 0 && right < s.length) { const leftLetter = s[left] const rightLetter = s[right] left-- right++ if(leftLetter === rightLetter) { currentLongest += 2 currentLongestSubStr = s.slice(left+1, right) } else break } if(currentLongest > longest) { if(currentLongestSubStr) longestSubStr = currentLongestSubStr else longestSubStr = s[i] + s[i+1] longest = currentLongest } } return longestSubStr } console.log(longestPalindrome("babad"))
0 likes • 0 views
const hammingDistance = (num1, num2) => ((num1 ^ num2).toString(2).match(/1/g) || '').length; hammingDistance(2, 3); // 1
• May 17, 2021 •LeifMessinger
//Upvotes comments according to a regex pattern. I tried it out on https://www.reddit.com/r/VALORANT/comments/ne74n4/please_admire_this_clip_precise_gunplay_btw/ //Known bugs: //If a comment was already upvoted, it gets downvoted //For some reason, it has around a 50-70% success rate. The case insensitive bit works, but I think some of the query selector stuff gets changed. Still, 50% is good let comments = document.getElementsByClassName("_3tw__eCCe7j-epNCKGXUKk"); //Comment class let commentsToUpvote = []; const regexToMatch = /wtf/gi; for(let comment of comments){ let text = comment.querySelector("._1qeIAgB0cPwnLhDF9XSiJM"); //Comment content class if(text == undefined){ continue; } text = text.textContent; if(regexToMatch.test(text)){ console.log(text); commentsToUpvote.push(comment); } } function upvote(comment){ console.log(comment.querySelector(".icon-upvote")); //Just showing you what it's doing comment.querySelector(".icon-upvote").click(); } function slowRecurse(){ let comment = commentsToUpvote.pop(); //It's gonna go bottom to top but whatever if(comment != undefined){ upvote(comment); setTimeout(slowRecurse,500); } } slowRecurse();
const binomialCoefficient = (n, k) => { if (Number.isNaN(n) || Number.isNaN(k)) return NaN; if (k < 0 || k > n) return 0; if (k === 0 || k === n) return 1; if (k === 1 || k === n - 1) return n; if (n - k < k) k = n - k; let res = n; for (let j = 2; j <= k; j++) res *= (n - j + 1) / j; return Math.round(res); }; binomialCoefficient(8, 2); // 28
• Jan 25, 2023 •C S
1 like • 17 views
export const Main = () => { const [title, setTitle] = useState(''); return ( <ChildComponent title={title} onChange={e => setTitle(e.target.value)} /> ); }; export const ChildComponent = ({ title, onChange }) => { return ( <Box component="form" onSubmit={() => {}} noValidate mt={5}> <TextField fullWidth id="title" label="Title" name="title" autoComplete="title" autoFocus color="primary" variant="filled" InputLabelProps={{ shrink: true }} value={title} onChange={onChange} /> </Box> ); };