xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js iterator protocol & next() All In One

js iterator protocol & next() All In One

.next()

// Satisfies both the Iterator Protocol and Iterable
const myIterator = {
    next: function() {
        // ...
    },
    [Symbol.iterator]: function() { return this; }
};

iterator protocol

迭代器协议

// .next()

// .next().value

// .next().done

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterator_protocol

iterators & generators

迭代器、生成器



https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators

demo


"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2020-11-26
 * @modified
 *
 * @description 146. LRU Cache
 * @description 146. LRU Cache
 * @difficulty Medium
 * @complexity O(n)
 * @time O(n)
 * @augments
 * @example
 * @link https://leetcode.com/problems/lru-cache/
 * @link https://leetcode-cn.com/problems/lru-cache/
 * @link https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU 最近最少使用(LRU)缓存算法
 * @solutions
 *
 * @best_solutions
 *
 */

const log = console.log;

/**
 * @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)) {
    return -1;
  } else {
    const value = this.map.get(key)
    // 读取后,更新顺序
    this.put(key, value);
    return value;
  }
  // if(this.map.has(key)) {
  //   const value = this.map.get(key)
  //   // 读取后,更新顺序
  //   this.put(key, value);
  //   return value;
  // } 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);
  }
  // 达到容量限制,先删除最近最少使用的 map 第一个元素
  if(this.map.size === this.capacity) {
    const oldKey = this.map.keys().next().value;
    this.map.delete(oldKey);
  }
  // 再重新添加
  this.map.set(key, 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)
 */



/*

lRUCache = new LRUCache(2);

lRUCache.put(1, 1);
// cache is {1=1}

lRUCache.put(2, 2);
// cache is {1=1, 2=2}

lRUCache.get(1);
// return 1

lRUCache.put(3, 3);
// LRU key was 2, evicts key 2, cache is {1=1, 3=3}

lRUCache.get(2);
// returns -1 (not found)

lRUCache.put(4, 4);
// LRU key was 1, evicts key 1, cache is {4=4, 3=3}

lRUCache.get(1);
// return -1 (not found)

lRUCache.get(3);
// return 3

lRUCache.get(4);
// return 4


*/


https://leetcode.com/problems/lru-cache/

https://leetcode.cn/problems/lru-cache/

refs

https://www.cnblogs.com/xgqfrms/p/16344654.html

https://leetcode.com/problems/lru-cache/discuss/2074758/Simple-Javascript-solution-or-Easy-to-understand



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-06-07 11:57  xgqfrms  阅读(38)  评论(3编辑  收藏  举报