【LeetCode】281. Zigzag Iterator 解题报告 (C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/zigzag-iterator/
题目描述
Given two 1d vectors, implement an iterator to return their elements alternately.
Example:
Input:
v1 = [1,2]
v2 = [3,4,5,6]
Output: [1,3,2,4,5,6]
Explanation: By calling next repeatedly until hasNext returns false,
the order of elements returned by next should be: [1,3,2,4,5,6].
Follow up: What if you are given k 1d vectors? How well can your code be extended to such cases?
Clarification for the follow up question:
The "Zigzag" order is not clearly defined and is ambiguous for k > 2 cases. If "Zigzag" does not look right to you, replace "Zigzag" with "Cyclic". For example:
Input:
[1,2,3]
[4,5,6,7]
[8,9]
Output: [1,4,8,2,5,9,3,6,7].
题目大意
给出两个一维的向量,请你实现一个迭代器,交替返回它们中间的元素。
解题方法
deque
看出题目的含义,有点类似于我们从不同链表中依次读取头部并删除头部的操作。所以可以使用数据结构来模拟这个操作,因此我们需要一个比较高效的能从头部删除元素的数据结构,比如双端队列deque。
具体做法是使用变量cur标识应该读取哪个deque,然后读取并删除该deque的头部,再修改变量cur。
C++代码如下:
class ZigzagIterator {
public:
ZigzagIterator(vector<int>& v1, vector<int>& v2) {
d1 = deque<int>(v1.begin(), v1.end());
d2 = deque<int>(v2.begin(), v2.end());
if (v1.empty())
cur = 1;
else
cur = 0;
}
int next() {
int val = 0;
if (cur == 0) {
val = d1.front(); d1.pop_front();
if (!d2.empty())
cur = 1;
} else if (cur == 1) {
val = d2.front(); d2.pop_front();
if (!d1.empty())
cur = 0;
}
return val;
}
bool hasNext() {
return !d1.empty() || !d2.empty();
}
private:
deque<int> d1, d2;
int cur = 0;
};
/**
* Your ZigzagIterator object will be instantiated and called as such:
* ZigzagIterator i(v1, v2);
* while (i.hasNext()) cout << i.next();
*/
日期
2019 年 9 月 24 日 —— 梦见回到了小学,小学已经芳草萋萋破败不堪
【LeetCode】281. Zigzag Iterator 解题报告 (C++)的更多相关文章
- [LeetCode] 281. Zigzag Iterator 之字形迭代器
Given two 1d vectors, implement an iterator to return their elements alternately. Example: Input: v1 ...
- [LeetCode#281] Zigzag Iterator
Problem: Given two 1d vectors, implement an iterator to return their elements alternately. For examp ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
随机推荐
- 4.Reverse Words in a String-Leetcode
class Solution { public: void reverseWords(string &s) { vector<string> data; string word; ...
- mybatis项目中,使用useSSL=true却报错
今天在玩儿mybatis的时候遇到一个蛮有东西的事情:抛了一个让我折腾几个小时的错误,所以记录一下 这个错误有意思的地方就在于这里: 当使用useSSL安全连接时,抛出了上述的错误把useSSL改为f ...
- admire, admit
admire 当别人admire你时,小心掉进泥潭(mire).词源:to wonder. wonderful夸人,awful骂人,awesome夸人.admiral与admire词源不同,碰巧长得像 ...
- 报错:Unsupported field: HourOfDay
报错:Unsupported field: HourOfDay 这个错误就比较搞笑也比较低级了. 代码如下 LocalDate now = LocalDate.now(); String year = ...
- web必知,多终端适配
导读 移动端适配,是我们在开发中经常会遇到的,这里面可能会遇到非常多的问题: 1px问题 UI图完美适配方案 iPhoneX适配方案 横屏适配 高清屏图片模糊问题 ... 上面这些问题可能我们在开发中 ...
- Identity Server 4 从入门到落地(八)—— .Net Framework 客户端
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- oracle to_char处理日期
select to_char(sysdate,'d') from dual;--本周第几天 select to_char(sysdate,'dd') from dual;--本月第几天 select ...
- 在调用系统相册时,UIIMagePickerController使用中偷换StatusBar颜色的问题
在调用系统相册时,UIIMagePickerController使用中偷换StatusBar颜色的问题 此时解决办法是 #pragma mark - UIImagePickerController D ...
- linux系统下安装dubbo-admin
1.在安装dubbo-admin之前确保你得linux服务器上已经成功安装了jdk,tomcat, 若还没安装jdk以及tomcat则参考我的上一篇文章"linux环境下安装jdk,tomc ...
- this指针的用法和基本分析
当在不同的对象中采用this指针,就已经是在给它赋值了.对象各自的this指针指向各自对象的首地址,所以不同对象的this指针一定指向不同的内存地址. this 指针是由系统自动提供的指向对象的特殊指 ...