2022.3.17
146. LRU 缓存
/**
 * @param {number} capacity
 */
var LRUCache = function(capacity) {
    this.capacity = capacity;
    this.map = new Map();
};
/** 
 * @param {number} key
 * @return {number}
 */
LRUCache.prototype.get = function(key) {
    // 当值存在时
    if(this.map.has(key)){
        const temp = this.map.get(key);
        this.map.delete(key);
        this.map.set(key, temp);
        return temp;
    }
    else {
        return -1;
    }
};
/** 
 * @param {number} key 
 * @param {number} value
 * @return {void}
 */
LRUCache.prototype.put = function(key, value) {
    // 当值存在,被访问时
    if(this.map.has(key)){
        this.map.delete(key);
    }
    this.map.set(key,value);
    if(this.map.size > this.capacity) {
        this.map.delete(this.map.keys().next().value);
    }
};
/**
 * Your LRUCache object will be instantiated and called as such:
 * var obj = new LRUCache(capacity)
 * var param_1 = obj.get(key)
 * obj.put(key,value)
 */
 

class LRUCache {
    constructor(capacity) {
        this.capacity = capacity;
        this.map = new Map();
    }
    get(key) {
        if (this.map.has(key)) {
            // get表示访问该值
            // 所以在访问的同时,要将其调整位置,放置在最后
            const temp = this.map.get(key);
            // 先删除,再添加
            this.map.delete(key);
            this.map.set(key, temp);
            // 返回访问的值
            return temp;
        } else {
            // 不存在,返回-1
            return -1;
        }
    }
    put(key, value) {
        // 要将其放在最后,所以若存在key,先删除
        if (this.map.has(key)) this.map.delete(key);
        // 设置key、value
        this.map.set(key, value);
        if (this.map.size > this.capacity) {
            // 若超出范围,将map中头部的删除
            // map.keys()返回一个迭代器
            // 迭代器调用next()方法,返回包含迭代器返回的下一个值,在value中
            this.map.delete(this.map.keys().next().value);
        }
    }
}
/**
 * Your LRUCache object will be instantiated and called as such:
 * var obj = new LRUCache(capacity)
 * var param_1 = obj.get(key)
 * obj.put(key,value)
 */
 
/**
 * @param {number} capacity
 */
var LRUCache = function(capacity) {
    this.capacity = capacity;
    this.map = new Map();
};
/** 
 * @param {number} key
 * @return {number}
 */
LRUCache.prototype.get = function(key) {
    if(this.map.has(key)){
        let temp=this.map.get(key)
         this.map.delete(key);
         this.map.set(key, temp);
         return temp
    }else{
        return -1
    }
};
/** 
 * @param {number} key 
 * @param {number} value
 * @return {void}
 */
LRUCache.prototype.put = function(key, value) {
    if(this.map.has(key)){
        this.map.delete(key);
    }
    this.map.set(key,value);
    if(this.map.size > this.capacity){
     
        this.map.delete(this.map.keys().next().value);
    }
};
/**
 * Your LRUCache object will be instantiated and called as such:
 * var obj = new LRUCache(capacity)
 * var param_1 = obj.get(key)
 * obj.put(key,value)
 */
 
2022.3.21
460. LFU 缓存
二、 653. 两数之和 IV - 输入 BST
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} k
 * @return {boolean}
 */
var findTarget = function(root, k) {
    const set = new Set();
    // const target = k - root.val
    const searchTree = (root, k) => {
        if(!root) {
            return false;
        }
        if(set.has(k-root.val)) {
            return true;
        }
        set.add(root.val)
        return searchTree(root.left,k) || searchTree(root.right,k)
    }
    return searchTree(root, k)
    
};
 

2022.3.22
一、 78. 子集

/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var subsets = function(nums) {
    let result = [];
    let num = [];
    const backtracking = (start) => {
        result.push(num.slice());
        for (let i = start; i< nums.length; i++){
            num.push(nums[i])
            backtracking(i+1);
            num.pop()
        }
    }
    backtracking(0);
    return result;
};
 
二、 77. 组合
/**
 * @param {number} n
 * @param {number} k
 * @return {number[][]}
 */
var combine = function(n, k) {
    let result = [];
    let track = [];
    const backtrack = (start) => {
        if(track.length === k){
            result.push(track.slice())
        }
        for(let i = start; i < n ;i++){
            // 做选择
            track.push(i+1);
            backtrack(i+1)
            // 撤销选择 
            track.pop()
        }
    }
    backtrack(0);
    return result
};
 

37. 解数独
36. 有效的数独
2022.3.24
一、 剑指 Offer II 056. 二叉搜索树中两个节点之和
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} k
 * @return {boolean}
 */
var findTarget = function(root, k) {
    const set = new Set();
    const helper = (root , k) => {
        if(root === null){
            return false;
        }
        if (set.has(k-root.val)) {
            return true;
        }
        set.add(root.val);
        return helper(root.left,k) || helper(root.right, k)
    }
    return helper(root,k);
};
 

二、 剑指 Offer II 052. 展平二叉搜索树
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {TreeNode}
 */
var increasingBST = function(root) {
    const res = [];
    const inorder = (root, res) => {
        if(root === null) return ;
        inorder(root.left, res);
        res.push(root.val);
        inorder(root.right, res);
    }
    inorder(root,res);
    const dummyNode = new TreeNode(-1);
    let curNode = dummyNode;
    for (const value of res) {
        curNode.right = new TreeNode(value);
        curNode = curNode.right;
    }
    return dummyNode.right;
    
};
 

三、 897. 递增顺序搜索树
2022.3.27
剑指 Offer 55 - I. 二叉树的深度
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var maxDepth = function (root) {
  // 定义:输入一个节点,返回以该节点为根的二叉树的最大深度
  if (root == null) return 0;
  return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
};
 











