leecode 解题总结:284. Peeking Iterator

来源:互联网 发布:风凰新闻软件 编辑:程序博客网 时间:2024/04/28 23:38
#include <iostream>#include <stdio.h>#include <vector>#include <string>using namespace std;/*问题:Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation -- it essentially peek() at the element that will be returned by the next call to next().Here is an example. Assume that the iterator is initialized to the beginning of the list: [1, 2, 3].Call next() gets you 1, the first element in the list.Now you call peek() and it returns 2, the next element. Calling next() after that still return 2.You call next() the final time and it returns 3, the last element. Calling hasNext() after that should return false.分析:此题本质上就是要你设置一个迭代器来对数组元素获取头部元素。不妨设定一个指针指向当前位置,每次调用next()的时候,指针指向下一个元素。而调用peek(),指针不发生改变,当调用hasNext()时,如果此时指针指向末尾就返回false,否则返回true。不能删除元素,这是遍历的操作结构弄错了:peek不是获取当前位置的元素,而是获取等同于执行下一次next操作的元素而next操作是获取当前位置元素,并设置位置累加输入:51 2 3 4 5输出:1 2 3 4 5报错:Input:[1,2,3,4][0,1,1,2,2,1,1,2,0,1,0,2,0]手动输出:true,1,1,1,2,3,3,3,true,4,true,4,falseOutput:["true","1","1","2","3","3","3","4","true","4","true","0","false"]Expected:["true","1","1","1","2","3","3","3","true","4","true","4","false"]0应该是hasNext()操作,1应该是peek操作,2应该是next()操作关键:1 弄错了:peek不是获取当前位置的元素,而是获取等同于执行下一次next操作的元素,即位置index + 1对应元素而next操作是先累加当前下标,然后获取该位置对应元素, int _index;//记录当前元素的下标,初始为-1,调用next是先先让_index++。然后返回该元素,peek返回 (_index + 1)对应元素所以初始化设置_index = -12 const vector<int>* _pArray;//数组指针,不用数组,必定用指针//但是如果不把数组拷贝过来,用index就无效了,不能用数组,那么用 指针指向数组的起始位置,//后面不断移动不就行了,如何判断移动到末尾,记录数组长度即可//是用vector指针指向3 //由于_index 是从-1开始的,获取下一个元素是需要累加,如果变成_size-1下一次累加就超过了if(_index < _size - 1)//最后一次执行_index变成_size - 1,之前是_size - 2*/// Below is the interface for Iterator, which is already defined for you.// **DO NOT** modify the interface for Iterator.class Iterator {public:Iterator(const vector<int>& nums){}Iterator(const Iterator& iter){}virtual ~Iterator(){}// Returns the next element in the iteration.int next(){return 0;}// Returns true if the iteration has more elements.bool hasNext() const{return true;}};class PeekingIterator : public Iterator {public:PeekingIterator(const vector<int>& nums) : Iterator(nums) {    // Initialize any member here.    // **DO NOT** save a copy of nums and manipulate it directly.    // You should only use the Iterator interface methods.    _size = nums.size();_index = -1;_pArray = &nums;}    // Returns the next element in the iteration without advancing the iterator.int peek() {//返回下一个元素        if(_index + 1 < _size){return _pArray->at(_index + 1);}else{return 0;}}// hasNext() and next() should behave the same as in the Iterator interface.// Override them if needed.int next() {    //获取下一个元素,迭代器发生变化_index++;if(_index < _size){return _pArray->at(_index);}else{return 0;}}bool hasNext() const {//由于_index 是从-1开始的,获取下一个元素是需要累加,如果变成_size-1下一次累加就超过了    if(_index < _size - 1)//最后一次执行_index变成_size - 1,之前是_size - 2{return true;}else{return false;}}private:int _size;//记录当前已经指向的元素下标,初始为0,记录int _index;//记录当前元素的下标,初始为-1,调用next是先先让_index++。然后返回该元素,peek返回 (_index + 1)对应元素const vector<int>* _pArray;//数组指针,不用数组,必定用指针//但是如果不把数组拷贝过来,用index就无效了,不能用数组,那么用 指针指向数组的起始位置,//后面不断移动不就行了,如何判断移动到末尾,记录数组长度即可//是用vector指针指向};void print(vector<int>& result){if(result.empty()){cout << "no result" << endl;return;}int size = result.size();for(int i = 0 ; i < size ; i++){cout << result.at(i) << " " ;}cout << endl;}void process(){ vector<int> nums; int value; int num; string command; int commandNum; while(cin >> num ) { nums.clear(); for(int i = 0 ; i < num ; i++) { cin >> value; nums.push_back(value); } const vector<int> nums2(nums); PeekingIterator peekIterator(nums2); while(peekIterator.hasNext()) { cout << peekIterator.peek() << " ";  //获取下一个元素 //peekIterator.next(); //cout << peekIterator.next() << " "; } }}int main(int argc , char* argv[]){//process();/*int arr[5] = {1,2,3,4,5};vector<int> nums;nums.insert(nums.begin() , arr , arr + 5);const vector<int> nums2(nums);const vector<int>* p = &nums2;int size = nums2.size();int count = 0;while(count < size){int value = p->at(count);count++;cout << value << endl;}*/getchar();return 0;}


0 0
原创粉丝点击