边工作边刷题: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 ...
随机推荐
- 部署时,出现用户代码未处理 System.Security.Cryptography.CryptographicException 错误解决方法
转载:http://www.cnblogs.com/jys509/p/4499978.html 在调用RSA加密的.pfx密钥时,在本地调试没有问题,可以布署到服务器,就会报以下的错误: 用户代码未处 ...
- java获取class所在jar
在类库的开发过程中,有些时候为了self-contain的原因,我们希望所有的资源都打包在jar中,但是有些工具好像无法支持从classpasth直接获取比如velocity的模板合并,此时我们就知道 ...
- mongodb c#语法基础
这里采用的是mongoDB官网推荐使用.net驱动: http://mongodb.github.io/mongo-csharp-driver/2.0/getting_started/quick_to ...
- OAUTH 协议介绍
OAUTH 产生背景 随着互联网的深入发展,一些互联网巨头积累了海量的用户和数据.对于平台级软件厂商来说,用户的需求多种多样,变化万千 以一己之力予以充分满足,难免疲于本命.因此将数据以接口的形式开放 ...
- 我在用的mac软件(2)-终端环境之zsh和z(*nix都适用)
继续上篇介绍我的终端环境.这篇介绍zsh和z,其实这不局限于os x,在所有的*nix系统中都是可用的. zsh zsh作为bash的替代品,自然很多人要问:why zsh? 在Zsh Worksho ...
- tableview直接滚动至最后一行的问题
tableview直接滚动至最后一行 类似聊天界面,tableview应该直接显示在最后一行,并且不应该有滚动的出现. 在网上查了很久,直接滚动至最后一行很容易实现,有两种方法比较好. 1. 调用sc ...
- iOS 解决表单被键盘遮住的问题
问题 处理表单的时候,一定会碰到的就是输入控件被键盘遮住的问题,如图: 实例 左边是普通表单,中间是2B表单,右边是文艺表单. 分析 处理这种问题无非就是2个步骤: 键盘弹出时,缩小UITableVi ...
- iOS设计模式-单例模式
(一)什么是单例模式(Singleton) 单例模式:保证一个类仅有一个实例,并提供一个访问它的全局访问点 *最初的定义是在<设计模式>(Addison-Wesley)中 解读 1> ...
- Android 网络编程基础之简单聊天程序
前一篇讲了Android的网络编程基础,今天写了一个简单的聊天程序分享一下 首先是服务端代码: package com.jiao.socketdemo; import java.io.Buffered ...
- UVa 102 - Ecological Bin Packing(规律,统计)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...