Last active
April 23, 2026 05:13
-
-
Save tangoabcdelta/5b3f4b89d8a2147ed1d667138d59708b to your computer and use it in GitHub Desktop.
Algos
This file contains hidden or 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://leetcode.com/problems/longest-substring-without-repeating-characters/description/ | |
| // "abcabcbb" ==> 3 | |
| // "bbbbb" ==> 1 | |
| // "pwwkew" ==> 3 | |
| function LongestNonRepeatedSubstringLength(str) { | |
| let h = new Map(); | |
| let left = 0; | |
| let maxLength = 0; | |
| for (let right = 0; right < str.length; right++) { | |
| let c = str[right]; | |
| if (h.has(c) && h.get(c) >= left) { | |
| left = h.get(c) + 1; | |
| } | |
| h.set(c, right); | |
| maxLength = Math.max(maxLength, right - left + 1); | |
| } | |
| console.log(str, maxLength); | |
| return maxLength; | |
| } | |
| LongestNonRepeatedSubstringLength("abcabcbb"); | |
| LongestNonRepeatedSubstringLength("bbbbb"); | |
| LongestNonRepeatedSubstringLength("pwwkew"); |
This file contains hidden or 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 MyLRUCache { | |
| constructor(size) { | |
| this.cache = new Map(); | |
| this.size = size; | |
| } | |
| get(key) { | |
| if (this.cache.has(key)) { | |
| let value = this.cache.get(key); | |
| this.cache.delete(key); | |
| this.cache.set(key, value); | |
| return value; | |
| } else { | |
| return null; | |
| } | |
| } | |
| put(key, value) { | |
| console.log("put", this.cache.set(key, value)); | |
| console.log(this.cache); | |
| if (this.cache.size > this.size) { | |
| console.log("delete", this.cache.delete(this.cache.keys().next().value)); | |
| console.log(this.cache); | |
| } | |
| } | |
| } | |
| let c = new MyLRUCache(3); | |
| c.put("1", "a"); | |
| c.put("2", "a"); | |
| c.put("2", "b"); | |
| c.put("3", "c"); | |
| // | |
| console.log("get", c.get("1")); | |
| c.put("4", "d"); | |
| // | |
| c.put("5", "e"); |
This file contains hidden or 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
| // foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment