当前位置: 面试刷题>> 左旋右旋迭代器 (经典算法题500道)


题目描述补充

题目:左旋右旋迭代器

给定一个数组(或列表)nums,设计一个迭代器,支持以下两种操作:

  1. next():返回数组中的下一个元素。如果所有元素都已被遍历过,则返回-1。
  2. rotateLeft():将数组中的所有元素向左旋转一个位置,即数组的第一个元素移动到数组的末尾。
  3. rotateRight():将数组中的所有元素向右旋转一个位置,即数组的最后一个元素移动到数组的开始位置。

注意

  • 初始时,迭代器指向数组的第一个元素。
  • 调用rotateLeft()rotateRight()时,不需要返回任何值,但会改变迭代器的内部状态,以便后续的next()调用能反映旋转后的数组顺序。

示例

假设数组为[1, 2, 3, 4, 5],以下是一系列操作及其结果:

iterator = LeftRightRotateIterator([1, 2, 3, 4, 5])
print(iterator.next())  # 输出: 1
iterator.rotateRight()
print(iterator.next())  # 输出: 5
print(iterator.next())  # 输出: 2
iterator.rotateLeft()
print(iterator.next())  # 输出: 3
iterator.rotateLeft()
print(iterator.next())  # 输出: 4
print(iterator.next())  # 输出: -1,因为所有元素都已遍历

PHP 示例代码

class LeftRightRotateIterator {
    private $nums;
    private $index = 0;

    function __construct($nums) {
        $this->nums = $nums;
    }

    function next() {
        if ($this->index >= count($this->nums)) {
            return -1;
        }
        $result = $this->nums[$this->index];
        $this->index++;
        return $result;
    }

    function rotateLeft() {
        if (count($this->nums) > 1) {
            $first = array_shift($this->nums);
            array_push($this->nums, $first);
            if ($this->index >= count($this->nums)) {
                $this->index = 0;
            }
        }
    }

    function rotateRight() {
        if (count($this->nums) > 1) {
            $last = array_pop($this->nums);
            array_unshift($this->nums, $last);
            if ($this->index >= count($this->nums)) {
                $this->index = 0;
            }
        }
    }
}

Python 示例代码

class LeftRightRotateIterator:
    def __init__(self, nums):
        self.nums = nums
        self.index = 0

    def next(self):
        if self.index >= len(self.nums):
            return -1
        result = self.nums[self.index]
        self.index += 1
        return result

    def rotateLeft(self):
        if len(self.nums) > 1:
            self.nums.append(self.nums.pop(0))
            if self.index >= len(self.nums):
                self.index = 0

    def rotateRight(self):
        if len(self.nums) > 1:
            self.nums.insert(0, self.nums.pop())
            if self.index >= len(self.nums):
                self.index = 0

JavaScript 示例代码

class LeftRightRotateIterator {
    constructor(nums) {
        this.nums = nums;
        this.index = 0;
    }

    next() {
        if (this.index >= this.nums.length) {
            return -1;
        }
        const result = this.nums[this.index];
        this.index++;
        return result;
    }

    rotateLeft() {
        if (this.nums.length > 1) {
            const first = this.nums.shift();
            this.nums.push(first);
            if (this.index >= this.nums.length) {
                this.index = 0;
            }
        }
    }

    rotateRight() {
        if (this.nums.length > 1) {
            const last = this.nums.pop();
            this.nums.unshift(last);
            if (this.index >= this.nums.length) {
                this.index = 0;
            }
        }
    }
}

码小课网站中有更多相关内容分享给大家学习,包括算法设计、数据结构、面试技巧等,欢迎访问学习。

推荐面试题