2023每日刷题(四十三)
Leetcode—1670.设计前中后队列
实现代码
erase(iterator position)在删除vector中的元素后,会将该元素的后面所有元素都往前挪一位。因此,原先的迭代器指向的元素就不是原来那个了,而是它的后一位元素
class FrontMiddleBackQueue {vector<int> v;
public:FrontMiddleBackQueue() {}void pushFront(int val) {v.insert(v.begin(), val);}void pushMiddle(int val) {int m = v.size() / 2;v.insert(v.begin() + m, val);}void pushBack(int val) {v.push_back(val);}int popFront() {if(v.size() == 0) {return -1;}int c = v[0];v.erase(v.begin());return c;}int popMiddle() {if(v.size() == 0) {return -1;}int m = (v.size() - 1)/ 2;int c = v[m];v.erase(v.begin()+m);return c;}int popBack() {if(v.size() == 0) {return -1;}int c = v[v.size() - 1];v.pop_back();return c;}
};/*** Your FrontMiddleBackQueue object will be instantiated and called as such:* FrontMiddleBackQueue* obj = new FrontMiddleBackQueue();* obj->pushFront(val);* obj->pushMiddle(val);* obj->pushBack(val);* int param_4 = obj->popFront();* int param_5 = obj->popMiddle();* int param_6 = obj->popBack();*/
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!