📣 独立日限时特惠: VIP通行证立减$68
楼主: 匿名
跳转到指定楼层
上一主题 下一主题
收起左侧

Google 电面第2面 9/11

🔗
peach=。= 2015-9-18 14:57:20 | 只看该作者
全局:
say543 发表于 2015-9-11 12:31
same solution. 感谢楼主分享

请问可以说详细一点吗?这题是要implement一个iterator还是返回arraylist呢?另外,从头取再放到队列尾巴是什么意思呢?求指教!
回复

使用道具 举报

🔗
douya 2015-9-20 10:52:35 | 只看该作者
全局:
KevinFromJail 发表于 2015-9-11 10:53
第二个问题是每次从头取, 再放到队列尾巴。空指针不放入。空队列停止。

这里这个iterator的意思是,next() 下一个list?
所以我大概写了下,这样对不对?

result
while(hasNext()){
    list = next();
    if(list.isEmpty()) break;
    result.add(list.get(0));
    list.remove(0);
}

return result;
回复

使用道具 举报

🔗
stellari 2015-9-20 20:03:24 | 只看该作者
全局:
douya 发表于 2015-9-20 10:52
这里这个iterator的意思是,next() 下一个list?
所以我大概写了下,这样对不对?

你大概是没理解这个题意。题目的意思并不是希望得到result这个完整序列,而是让你实现AlternateIterator::hasNext()和AternateIterator::next()这两个函数。题目中本来可以给List<List<Type> >, 但是实际上却用Iterator替换掉了内层的List,稍微增加了一些难度,但是算法本质是一样的。这道题的输入甚至还可以可给成Iterator<Iterator<Type> >,做法都是一样的。
回复

使用道具 举报

🔗
douya 2015-9-21 04:58:34 | 只看该作者
全局:
stellari 发表于 2015-9-20 20:03
你大概是没理解这个题意。题目的意思并不是希望得到result这个完整序列,而是让你实现AlternateIterator: ...

哦,多谢高人指点。我大概明白了。我试着写了下。
您看这样写对不对:
Class MyIterator{

    List<Iterator> input;
    int index=0;
    public MyIterator(List<Iterator> input){
        this. input = input;      
    }
    public int next(){
        Iterator iter=input.get(index);
        int result;
        if(iter.hasNext()){
            result=iter.next();
        }
        if(!iter.hasNext()) {
            input.remove(iter);
        } else {
            index=(index+1)%input.size();
        }
        return result;
    }

    public boolean hasNext(){
        return !input.isEmpty();
    }
}
回复

使用道具 举报

🔗
stellari 2015-9-21 13:04:02 | 只看该作者
全局:
douya 发表于 2015-9-21 04:58
哦,多谢高人指点。我大概明白了。我试着写了下。
您看这样写对不对:
Class MyIterator{

这个是通不过的。如果List中所有的Iterator都为空,那么第一次调用hasNext()会返回什么?应该返回什么?

回复

使用道具 举报

🔗
douya 2015-9-22 07:05:17 | 只看该作者
全局:
stellari 发表于 2015-9-21 13:04
这个是通不过的。如果List中所有的Iterator都为空,那么第一次调用hasNext()会返回什么?应该返回什么?
...

有道理!我去改改,多谢!多谢!
回复

使用道具 举报

🔗
uuuouou 2015-9-22 10:36:38 | 只看该作者
全局:
谢lz面经,分享个第二题C++的solution
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>
  4. #include <list>
  5. using namespace std;

  6. class InterleavingIterator{
  7. private:
  8.         typedef vector<int>::const_iterator CI;
  9.         typedef pair<CI, CI> Cursor;
  10.         typedef list<Cursor>::iterator Iter;
  11. private:
  12.         list<Cursor> cursorList;
  13.         Iter         iter;
  14. public:
  15.         InterleavingIterator(const vector<vector<int> >& arrays){
  16.                 for(int i = 0; i < arrays.size(); ++i){
  17.                         if(!arrays[i].empty()){
  18.                                 cursorList.push_back(Cursor(arrays[i].begin(), arrays[i].end()));
  19.                         }
  20.                 }
  21.                 iter = cursorList.begin();
  22.         }
  23.         bool hasNext()const{
  24.                 return !cursorList.empty();
  25.         }
  26.         int next(){
  27.                 int x = *(*iter).first++;
  28.                 if((*iter).first == (*iter).second){
  29.                         iter = cursorList.erase(iter);
  30.                 }
  31.                 else{
  32.                         ++iter;
  33.                 }
  34.                 if(iter == cursorList.end()) iter = cursorList.begin();
  35.                 return x;
  36.         }
  37. };

  38. int main()
  39. {
  40.         int a[] = {1, 2, 3};
  41.         int b[] = {4, 5, 6};
  42.         int c[] = {7, 8};

  43.         vector<vector<int> > arrays;
  44.         arrays.push_back(vector<int>(a, a + sizeof(a)/sizeof(int)));
  45.         arrays.push_back(vector<int>(b, b + sizeof(b)/sizeof(int)));
  46.         arrays.push_back(vector<int>(c, c + sizeof(c)/sizeof(int)));

  47.         InterleavingIterator iter(arrays);
  48.         while(iter.hasNext()){
  49.                 cout << iter.next() << " ";
  50.         }

  51.         return 0;
  52. }
复制代码
回复

使用道具 举报

🔗
uuuouou 2015-9-22 10:57:52 | 只看该作者
全局:
额,修改一下,可能看起来更舒服些
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>
  4. #include <list>
  5. using namespace std;

  6. class Cursor{
  7.         typedef vector<int>::const_iterator CI;
  8. private:
  9.         CI cur, end;
  10. public:
  11.         Cursor(const vector<int>& arr){
  12.                 cur = arr.begin();
  13.                 end = arr.end();
  14.         }
  15.         bool hasNext()const{
  16.                 return cur != end;
  17.         }
  18.         int next(){
  19.                 return *cur++;
  20.         }
  21. };

  22. class InterleavingIterator{
  23.         typedef list<Cursor>::iterator Iter;
  24. private:
  25.         list<Cursor> cursorList;
  26.         Iter         iter;
  27. public:
  28.         InterleavingIterator(const vector<vector<int> >& arrays){
  29.                 for(int i = 0; i < arrays.size(); ++i){
  30.                         if(!arrays[i].empty()){
  31.                                 cursorList.push_back(Cursor(arrays[i]));
  32.                         }
  33.                 }
  34.                 iter = cursorList.begin();
  35.         }
  36.         bool hasNext()const{
  37.                 return !cursorList.empty();
  38.         }
  39.         int next(){
  40.                 int x = (*iter).next();
  41.                 if((*iter).hasNext()){
  42.                         ++iter;
  43.                 }
  44.                 else{
  45.                         iter = cursorList.erase(iter);
  46.                 }
  47.                 if(iter == cursorList.end()) iter = cursorList.begin();
  48.                 return x;
  49.         }
  50. };

  51. int main()
  52. {
  53.         int a[] = {1, 2, 3};
  54.         int b[] = {4, 5, 6};
  55.         int c[] = {7, 8};

  56.         vector<vector<int> > arrays;
  57.         arrays.push_back(vector<int>(a, a + sizeof(a)/sizeof(int)));
  58.         arrays.push_back(vector<int>(b, b + sizeof(b)/sizeof(int)));
  59.         arrays.push_back(vector<int>(c, c + sizeof(c)/sizeof(int)));

  60.         InterleavingIterator iter(arrays);
  61.         while(iter.hasNext()){
  62.                 cout << iter.next() << " ";
  63.         }

  64.         return 0;
  65. }
复制代码
回复

使用道具 举报

🔗
stellari 2015-9-22 13:10:39 | 只看该作者
全局:
uuuouou 发表于 2015-9-22 10:57
额,修改一下,可能看起来更舒服些

感谢分享!不过我觉得InterleavingIterator最好是能继承自一个Iterator接口(在C++中是纯虚类)。cursorList不是list<Cursor>类型,而是list<Iterator*>(因为传进来的参数就是list<Iterator*>),这样的好处是:具有Iterator接口的InterleavingIterator本身又可以被其他Iterator包装,因此可以轻松实现各种Iterator方式的组合。比如InterleavingIterator被EvenIterator(只返回偶数的Iterator)包装的话,就能够实现“以Interleaving方式取数字,但是要隔一行取一个这种方式”。然后你再跟面试官说一句,“Essentially I just implemented the Decorator Pattern”,他可能就会觉得你很懂OO设计了。
回复

使用道具 举报

🔗
uuuouou 2015-9-22 16:45:51 | 只看该作者
全局:
stellari 发表于 2015-9-22 13:10
感谢分享!不过我觉得InterleavingIterator最好是能继承自一个Iterator接口(在C++中是纯虚类)。cursorL ...

很有道理的会说,感谢指点~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表