IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

顶端迭代器

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

顶端迭代器

给定一个迭代器类的接口,接口包含两个方法:next()hasNext()。设计并实现一个支持peek()操作的顶端迭代器--其本质就是把原本应由next()方法返回的元素peek()出来。

示例

假设迭代器被初始化为列表 [1,2,3]。

调用 next() 返回 1,得到列表中的第一个元素。
现在调用 peek() 返回 2,下一个元素。在此之后调用 next() 仍然返回 2。
最后一次调用 next() 返回 3,末尾元素。在此之后调用 hasNext() 应该返回 false。

题解

/**
 * // This is the Iterator's API interface.
 * // You should not implement it, or speculate about its implementation.
 * function Iterator() {
 *    @ return {number}
 *    this.next = function() { // return the next number of the iterator
 *       ...
 *    }; 
 *
 *    @return {boolean}
 *    this.hasNext = function() { // return true if it still has numbers
 *       ...
 *    };
 * };
 */

/**
 * @param {Iterator} iterator
 */
var PeekingIterator = function(iterator) {
    this.iterator = iterator;
    this.cache = null;
};

/**
 * @return {number}
 */
PeekingIterator.prototype.peek = function() {
    if(this.cache !== null) return this.cache;
    var cache = this.iterator.next();
    this.cache = cache;
    return cache;
};

/**
 * @return {number}
 */
PeekingIterator.prototype.next = function() {
    if(this.cache !== null) {
        var cache = this.cache;
        this.cache = null;
        return cache;
    }
    return this.iterator.next();
};

/**
 * @return {boolean}
 */
PeekingIterator.prototype.hasNext = function() {
    if(this.cache !== null) return true;
    return this.iterator.hasNext();
};

/** 
 * Your PeekingIterator object will be instantiated and called as such:
 * var obj = new PeekingIterator(arr)
 * var param_1 = obj.peek()
 * var param_2 = obj.next()
 * var param_3 = obj.hasNext()
 */

思路

本题的意思是现在已经有一个迭代器iterator,其实现了两个方法next()hasNext(),需要通过这两个方法实现一个新的迭代器方法peek(),通过实现PeekingIterator构造函数以及new实例化构造函数,那么在PeekingIterator构造函数的原型链上实现next()hasNext()peek()方法即可,实例化对象时赋值传入的iterator对象的引用到iterator属相中,定义cache属性作为缓存并初始化赋值为null,首先实现peek()方法,在调用该方法时首先取出迭代器对象的next值,并将其写到cache属性中然后返回该值,然后实现next()方法,首先判断cache是否为null,存在缓存则将缓存置为null且返回缓存值,不存在缓存则直接调用迭代器的next()方法,同样对于hasNext()方法的实现,首先判断缓存是否存在,如果存在缓存则认为并未迭代完成,若不存在缓存则调用迭代器的hasNext()进行判断。

每日一题

https://github.com/WindrunnerMax/EveryDay

题源

https://leetcode-cn.com/problems/peeking-iterator/

文章永久链接:https://tech.souyunku.com/?p=39653


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(61) 打赏



未经允许不得转载:搜云库技术团队 » 顶端迭代器

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367