Zigzag Iterator II
Description
k
1d vectors? How well can your code be extended to such cases? 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".Example
Example1
Input: k = 3
vecs = [
[1,2,3],
[4,5,6,7],
[8,9],
]
Output: [1,4,8,2,5,9,3,6,7]
Example2
Input: k = 3
vecs = [
[1,1,1]
[2,2,2]
[3,3,3]
]
Output: [1,2,3,1,2,3,1,2,3]
思路:
用k个指针维护每个数组的当前最靠前的元素,循环枚举这k个指针,每次枚举到就在答案序列中将该数加入,如果指针到了该数组的尾部,那么就不作处理
public class ZigzagIterator2 { public List<Iterator<Integer>> its;
public int turns; /**
* @param vecs a list of 1d vectors
*/
public ZigzagIterator2(List<List<Integer>> vecs) {
// initialize your data structure here.
this.its = new ArrayList<Iterator<Integer>>();
for (List<Integer> vec : vecs) {
if (vec.size() > 0)
its.add(vec.iterator());
}
turns = 0;
} public int next() {
// Write your code here
int elem = its.get(turns).next();
if (its.get(turns).hasNext())
turns = (turns + 1) % its.size();
else {
its.remove(turns);
if (its.size() > 0)
turns %= its.size();
}
return elem;
} public boolean hasNext() {
// Write your code here
return its.size() > 0;
}
}
/**
* Your ZigzagIterator2 object will be instantiated and called as such:
* ZigzagIterator2 solution = new ZigzagIterator2(vecs);
* while (solution.hasNext()) result.add(solution.next());
* Output result
*/
Zigzag Iterator II的更多相关文章
- 281. Zigzag Iterator
题目: Given two 1d vectors, implement an iterator to return their elements alternately. For example, g ...
- [Locked] Zigzag Iterator
Zigzag Iterator Given two 1d vectors, implement an iterator to return their elements alternately. Fo ...
- [LeetCode] Zigzag Iterator 之字形迭代器
Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...
- Zigzag Iterator
Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...
- LeetCode Zigzag Iterator
原题链接在这里:https://leetcode.com/problems/zigzag-iterator/ 题目: Given two 1d vectors, implement an iterat ...
- [LeetCode#281] Zigzag Iterator
Problem: Given two 1d vectors, implement an iterator to return their elements alternately. For examp ...
- [Swift]LeetCode281. 之字形迭代器 $ Zigzag Iterator
Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...
- 281. Zigzag Iterator z字型遍历
[抄题]: Given two 1d vectors, implement an iterator to return their elements alternately. Example: Inp ...
- [LeetCode] 281. Zigzag Iterator 之字形迭代器
Given two 1d vectors, implement an iterator to return their elements alternately. Example: Input: v1 ...
随机推荐
- 【java】java 读写文件
场景:JDK8 将上传的文件,保存到服务器 Java读写文件操作: MultipartFile file InputStream inputStream = file.getInputStream( ...
- Java开发月薪2W的知乎讨论记录截取
1. 推荐看 作者:匿名用户 链接:https://www.zhihu.com/question/39890405/answer/83676977 来源:知乎 著作权归作者所有.商业转载请联系作者获得 ...
- Spring Boot 主从读写分离
自己封装了一个读写分离的 Starter,可以配置任意多个数据源,使用 Hikari 连接池(暂不支持其他连接池). GitHub:rw-separate-spring-boot-starter 代码 ...
- mysql提示The server quit without updating PID file /usr/local/mysql/data/localhost.localdomain.pid
chown -R mysql:mysql /var/lib/mysql 解决方法 :给予权限,执行 “chown -R mysql:mysql /var/lib/mysql” “chmod -R 75 ...
- SQL Server外键关系是强制约束,外键值也可以是空(NULL)
在SQL Server中,实际上外键值可不可以为空(NULL),和外键关系是不是强制约束无关. 我们先在SQL Server数据库中建立两张表People和Car,一个People可以有多个Car,所 ...
- String类的方法应用
String类的几个方法的应用示例: using System;using System.Collections.Generic;using System.Linq;using System.Text ...
- JS中判断对象是对象还是数组的方法
https://www.cnblogs.com/ma-shuai/p/7805264.html
- 转 根据CPU核心数确定线程池并发线程数
转自: https://www.cnblogs.com/dennyzhangdd/p/6909771.html?utm_source=itdadao&utm_medium=referral 目 ...
- 后缀表达式 Java实现
基本原理: 从左到右扫描字符串:1.是操作数:压栈. 2.是操作符:出栈两个操作数,将运算结果压栈. 扫描字符串通过java.util.Scanner类实现,其next方法可以读取以空格(默认)或指定 ...
- C++使用通配符查找文件(FindFirstFile)
调用 FindFirstFile 和 FindNextFile 可搜索某个目录下的相应文件. BOOL SearchFilesByWildcard(WCHAR *wildcardPath) { HAN ...