边工作边刷题:70天一遍leetcode: day 84
Flatten 2D Vector
要点:
- 这题是2d的iterator,一般对于1d的情况,hasNext()是不需要做移动的。而2d不同,core iterator是j向的,而i向要在hasNext()中移动以保证call next()的时候j是available的。
- hasNext()如何移动?首先的思路是not j.hasNext()是触发i移动的条件,同时j本身也可能是None(比如Null 2d list)。简单的说就是不算移动i直到j没超界,如果i都超界了,那么return False. 这个条件下还要判断i.hasNext(),然后移动i,赋值j iterator。当这步之后还是not j.hasNext()。那么说明遍历完毕。
- 同样这题无论是用iterator还是index都是同样思路,index的时候hasNext()要先判断i,然后才有机会判断j。而iterator version,一个0元素list仍然有iterator,但是i.hasNext()为False。用j iterator为None来表示i根本没初始化
# 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].
# Hint:
# How many variables do you need to keep track?
# Two variables is all you need. Try with x and y.
# Beware of empty rows. It could be the first few rows.
# To write correct code, think about the invariant to maintain. What is it?
# The invariant is x and y must always point to a valid point in the 2d vector. Should you maintain your invariant ahead of time or right when you need it?
# Not sure? Think about how you would implement hasNext(). Which is more complex?
# Common logic in two different places should be refactored into a common method.
# Follow up:
# As an added challenge, try to code it using only iterators in C++ or iterators in Java.
# Hide Company Tags Google Airbnb Twitter Zenefits
# Hide Tags Design
# Hide Similar Problems (M) Binary Search Tree Iterator (M) Zigzag Iterator (M) Peeking Iterator (M) Flatten Nested List Iterator
class Vector2D(object):
def __init__(self, vec2d):
"""
Initialize your data structure here.
:type vec2d: List[List[int]]
"""
self.vec = vec2d
self.i = 0
self.j = 0
def next(self):
"""
:rtype: int
"""
ret = self.vec[self.i][self.j]
self.j+=1
return ret
def hasNext(self):
"""
:rtype: bool
"""
while self.i<len(self.vec):
if self.j<len(self.vec[self.i]):
return True
self.i+=1
self.j=0
return False
# Your Vector2D object will be instantiated and called as such:
# i, v = Vector2D(vec2d), []
# while i.hasNext(): v.append(i.next())
边工作边刷题:70天一遍leetcode: day 84的更多相关文章
- 边工作边刷题:70天一遍leetcode: day 89
Word Break I/II 现在看都是小case题了,一遍过了.注意这题不是np complete,dp解的time complexity可以是O(n^2) or O(nm) (取决于inner ...
- 边工作边刷题:70天一遍leetcode: day 77
Paint House I/II 要点:这题要区分房子编号i和颜色编号k:目标是某个颜色,所以min的list是上一个房子编号中所有其他颜色+当前颜色的cost https://repl.it/Chw ...
- 边工作边刷题:70天一遍leetcode: day 78
Graph Valid Tree 要点:本身题不难,关键是这题涉及几道关联题目,要清楚之间的差别和关联才能解类似题:isTree就比isCycle多了检查连通性,所以这一系列题从结构上分以下三部分 g ...
- 边工作边刷题:70天一遍leetcode: day 85-3
Zigzag Iterator 要点: 实际不是zigzag而是纵向访问 这题可以扩展到k个list,也可以扩展到只给iterator而不给list.结构上没什么区别,iterator的hasNext ...
- 边工作边刷题:70天一遍leetcode: day 101
dp/recursion的方式和是不是game无关,和game本身的规则有关:flip game不累加值,只需要一个boolean就可以.coin in a line II是从一个方向上选取,所以1d ...
- 边工作边刷题:70天一遍leetcode: day 1
(今日完成:Two Sum, Add Two Numbers, Longest Substring Without Repeating Characters, Median of Two Sorted ...
- 边工作边刷题:70天一遍leetcode: day 70
Design Phone Directory 要点:坑爹的一题,扩展的话类似LRU,但是本题的accept解直接一个set搞定 https://repl.it/Cu0j # Design a Phon ...
- 边工作边刷题:70天一遍leetcode: day 71-3
Two Sum I/II/III 要点:都是简单题,III就要注意如果value-num==num的情况,所以要count,并且count>1 https://repl.it/CrZG 错误点: ...
- 边工作边刷题:70天一遍leetcode: day 71-2
One Edit Distance 要点:有两种解法要考虑:已知长度和未知长度(比如只给个iterator) 已知长度:最好不要用if/else在最外面分情况,而是loop在外,用err记录misma ...
随机推荐
- 【BZOJ 4551】【TJOI2016】【HEOI2016】树
http://www.lydsy.com/JudgeOnline/problem.php?id=4551 题目描述 给定一棵有根树(根为 1),有以下两种操作:1. 标记操作:对某个结点打上标记(在最 ...
- bootstrap 学习片段
1. 只要单击按钮添加了data-toggle="dropdown"属性, 在单击按钮的时候,默认隐藏的下拉列表就会显示出来 <div class="row&quo ...
- 为什么重新设计 ASP.NET?
灵活的跨平台运行时需求 早期 .NET Framework 版本一直作为单一且全面的整体进行安装,每个新版本都包含了新功能和几乎所有早期功能,而鲜有删减,这就不可避免的造成Framework的体积的增 ...
- ASP.NET页面间传值总结
本文我们将讨论的是ASP.NET页面间数据传递的几种方法,对此希望能帮助大家正确的理解ASP.NET页面间数据传递的用处以及便利性. Web页面是无状态的,服务器对每一次请求都认为来自不同用户,因此, ...
- For循环语句的使用
一.For循环语句 说明:For循环用于循环次数已经确定的情况下. 格式:for(循环变量赋初值; 循环条件; 循环变量增值) { ·····语句 } 举例:求 ...
- Force.com微信开发系列(二)用户消息处理
Force.com是国际知名的云平台公司,成功配置好Force.com作为微信公开号的服务端后,接下来需要的任务是处理用户发送的消息.当普通微信用户向公众账号发消息时,微信服务器将POST消息的XML ...
- ListView属性整理
stackFromBottom属性,这只该属性之后你做好的列表就会显示你列表的最下面,值为true和false android:stackFromBottom="true" 第 ...
- c++ const用法小结
const用法 1,定义全局变量的内存分配问题 #define Pi_1 3.14 //使用#define宏 const double Pi_2 = 3.14 //使用const ...
- 自定义控件--CircleImageView(类似于QQ、微信圆形头像自定义控件)
现在基本上所有的需要用户注册的APP都有一个需要用户上传头像的需求,上传的头像基本都是类似于QQ.微信等社交应用圆形头像.最近,正在做的一个社交应用多处需要用到这种圆形头像的处理,总不能每次都对图片做 ...
- float类型转对象 对象转float类型(一)
//float类型转化为对象CGFloat fValue = 1.f;NSNumber *objNo = [NSNumber numberWithFloat:fValue];数值.BOOL型都可以转成 ...