由买买提看人间百态

topics

全部话题 - 话题: listit
(共0页)
f*********m
发帖数: 726
1
来自主题: JobHunting版 - 谷歌面经
第二题贴个template List版的,请各位指正:
template
class NextClass
{
public:
NextClass(list >& L):CList (L), listListIt(L.begin()), listIt((*L.
begin()).begin())
{
while (listlistIt != CList.end())
{
if ((*listlistIt).begin() != (*listlistIt).end())
{
listIt = (*listlistIt).begin();
break;
}
++listlistIt;
}
if ((*listlistIt).begin() == (*listlistIt).end())
c... 阅读全帖
f*******5
发帖数: 52
2
如果用python的话stack里不用放当前下标。当栈顶是数组的话就弹出数组, 然后逆序
入栈。以下是python代码,假定只有list 和 basic type 两种类型
class ListIter(object):
def __init__(self, lst):
self.stack = [lst]
def next(self):
if len(self.stack) == 0:
raise StopIteration
while isinstance(self.stack[-1],list):
l = self.stack.pop()
self.stack.extend([i for i in reversed(l) if not isinstance(i,
list) or len(i)>0])
if len(self.stack) == 0:
raise StopIteration
... 阅读全帖
(共0页)