Problem Description:
Implement an iterator to flatten a 2d vector.
For example,
Given 2d vector =
[
[1,2],
[3],
[4,5,6]
]
By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1, 2, 3, 4, 5, 6].
代码:
class Iterator {
private:
int i, j;
vector<vector<int> > v;
public:
Iterator(vector<vector<int> > &vv) {
i = j = ;
copy(vv.begin(), vv.end(), v.begin());
}
bool hasNext() {
return i < v.size();
}
int next() {
int nextVal = v[i][j];
if(++j == v[i].size()) {
j = ;
++i;
}
return nextVal;
}
};
[Locked] Flatten 2D Vector的更多相关文章
-
[LeetCode] Flatten 2D Vector 压平二维向量
Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...
-
Flatten 2D Vector
Implement an iterator to flatten a 2d vector. For example, Given 2d vector = [ [1,2], [3], [4,5,6] ] ...
-
LeetCode Flatten 2D Vector
原题链接在这里:https://leetcode.com/problems/flatten-2d-vector/ 题目: Implement an iterator to flatten a 2d v ...
-
251. Flatten 2D Vector
题目: Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6 ...
-
[Swift]LeetCode251.展平二维向量 $ Flatten 2D Vector
Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...
-
251. Flatten 2D Vector 平铺二维矩阵
[抄题]: Implement an iterator to flatten a 2d vector. Example: Input: 2d vector = [ [1,2], [3], [4,5,6 ...
-
LeetCode 251. Flatten 2D Vector
原题链接在这里:https://leetcode.com/problems/flatten-2d-vector/ 题目: Implement an iterator to flatten a 2d v ...
-
Flatten 2D Vector -- LeetCode
Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [,], [], [,,] ] By cal ...
-
[LeetCode] 251. Flatten 2D Vector 压平二维向量
Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...
随机推荐
-
Android的NDK技术
Android的NDK技术
-
Qt 释放新建窗口资源
当Widget *w = new Widget(); 不方便调用 delete w;时 Widget *w = new Widget(); w->setAttribute(Qt::WA_Dele ...
-
Android lint具 常见问题检查
1. Correctness 1) DuplicatedIds Layout于id应该唯一 2) NewApi 代码中使用的某些API高于Manifest中的Min SDK 3) Inconsiste ...
-
6 python高级数据处理和可视化
6.2. pyplot作图 1.折线图和散点图 t = np.arange(0,4,0.1) plt.plot(t,t,'o',t,t+2,t,t**2,'o') plt.show() 2.柱线图 p ...
-
layui loading
layer.msg('加载中', { icon: 16 ,shade: 0.4}); layer.load(2);风格二 setTimeout(function(){ layer.closeAll(' ...
-
做到让DBCP连接池不超时
前些天部署了一个项目,但每次隔一段时间打开都会报如下所示的错误: javax.servlet.ServletException: org.springframework.transaction.Ca ...
-
retrofit 基础使用
1.先导入依赖 加上网络权限 <uses-permission android:name="android.permission.INTERNET" /> 2.创建接口 ...
-
POJ3189 Steady Cow Assignment
Steady Cow Assignment Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6817 Accepted: ...
-
bigdecimal 与long int 之间转换
BigDecimal与Long.int之间的互换 在实际开发过程中BigDecimal是一个经常用到的数据类型,它和int Long之间可以相互转换. 转换关系如下代码展示: int 转换成 BigD ...
-
配置linux DNS
DNS服务器地址配置 在Linux下面,有一个默认的DNS服务器地址配置文件的设置,存放在 /etc/resolv.conf 设置方法很简单,通过编辑 vi /etc/resolv.conf 设置首选 ...