var lengthOfLongestSubstring = function (s) {
let map = new Map();
let maxLen = 0;
let left = 0;
for (let i = 0; i < s.length; i++) {
if (map.has(s[i]) && map.get(s[i]) >= left) {
left = map.get(s[i]) + 1;
}
map.set(s[i], i);
maxLen = Math.max(maxLen, i - left + 1);
}
return maxLen
};
class ListNode {
constructor(key, value) {
this.key = key;
this.value = value;
this.prev = null;
this.next = null;
}
}
class LRUCache {
constructor(capacity) {
this.capacity = capacity;
this.hash = {};
this.count = 0;
this.dummyHead = new ListNode()
this.dummyTail = new ListNode()
this.dummyHead.next = this.dummyTail;
this.dummyTail.prev = this.dummyHead;
}
get(key) {
let node = this.hash[key];
if (!node) return -1;
this.deleteNode(node);
this.addToHead(node);
return node.value;
}
put(key, value) {
let node = this.hash[key];
if (node) {
node.value = value;
this.deleteNode(node);
this.addToHead(node);
} else {
if (this.count == this.capacity) {
this.deleteTailNode();
}
let newNode = new ListNode(key, value);
this.hash[key] = newNode;
this.count++;
this.addToHead(newNode)
}
}
deleteNode(node) {
let temp1 = node.prev;
let temp2 = node.next;
temp1.next = temp2;
temp2.prev = temp1;
}
addToHead(node) {
let temp = this.dummyHead.next;
this.dummyHead.next = node;
node.prev = this.dummyHead;
node.next = temp;
temp.prev = node
}
deleteTailNode() {
let tail = this.dummyTail.prev;
this.deleteNode(tail);
delete this.hash[tail.key];
this.count--;
}
}
var LRUCache = function(capacity) {
this.limit = capacity;
this.cache = new Map();
};
LRUCache.prototype.get = function(key) {
let tmp;
if(this.cache.has(key)){
tmp = this.cache.get(key);
this.cache.delete(key);
this.cache.set(key,tmp);
}
return tmp ?? -1;
};
LRUCache.prototype.put = function(key, value) {
if(this.cache.has(key)){
this.cache.delete(key);
}
this.cache.set(key,value);
if(this.cache.size > this.limit){
this.cache.delete(this.cache.keys().next().value)
}
};
var longestPalindrome = function(s) {
if (s.length<2){
return s
}
let res = ''
for (let i = 0; i < s.length; i++) {
helper(i, i)
helper(i, i + 1)
}
function helper(m, n) {
while (m >= 0 && n < s.length && s[m] == s[n]) {
m--
n++
}
if (n - m - 1 > res.length) {
res = s.slice(m + 1, n)
}
}
return res
};
var twoSum = function (nums, target) {
let map = new Map()
for (let i = 0; i < nums.length; i++) {
if (map.has(target - nums[i])) {
let num = map.get(target - nums[i])
return [num,i]
} else {
map.set(nums[i], i)
}
}
return [];
};
var twoSum = function(nums, target) {
for(let i=0;i<nums.length;i++){
for(let j=i+1;j<nums.length;j++){
if(nums[i]+nums[j] == target){
return [i,j]
}
}
}
};
var threeSum = function (nums) {
let res = []
nums.sort((a, b) => a - b);
for (let i = 0; i < nums.length; i++) {
if (nums[i] > 0) break;
if (i > 0 && nums[i] === nums[i - 1]) continue;
let left = i + 1;
let right = nums.length - 1;
while (left < right) {
if (nums[i] + nums[left] + nums[right] === 0) {
res.push([nums[i], nums[left], nums[right]])
while (left < right && nums[left] === nums[left + 1]) left++;
while (left < right && nums[right] === nums[right - 1]) right--;
left++;
right--;
} else if (nums[i] + nums[left] + nums[right] > 0) {
right--;
} else {
left++;
}
}
}
return res;
};
var reverseList = function (head) {
let cur = head;
let prev = null;
while (cur) {
let nextNode = cur.next;
cur.next = prev;
prev = cur;
cur = nextNode;
}
return prev;
};