楼主: ecneralc
跳转到指定楼层
上一主题 下一主题
收起左侧

发个AirBnb电面跪经

🔗
 楼主| ecneralc 2017-5-9 07:36:44 | 只看该作者
全局:
runningMajia 发表于 2017-5-9 07:17
arraylist的长度最多为10 这个指的所有arraylist么?
如果不是的话,那只需要maintain一个2d的ArrayList o ...

是所有的arraylist,所以感觉很麻烦的样子
回复

使用道具 举报

🔗
 楼主| ecneralc 2017-5-9 07:37:32 | 只看该作者
全局:
382851453 发表于 2017-5-9 06:02
楼主可以看下circular buffer,感觉可以用双指针解决

你是说一个headIndex一个tailIndex吗?是这样的,考虑到嵌套arraylist还是好复杂的感觉
回复

使用道具 举报

🔗
runningMajia 2017-5-9 07:46:14 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
 楼主| ecneralc 2017-5-9 08:45:30 | 只看该作者
全局:
runningMajia 发表于 2017-5-9 07:46
不用嵌套的话, 可以试试HashMap, key 是从0到n记录目前有多少个list的id,value就是最大长度为10的Array ...

我提过这个方法,面试官提示说HashMap的实现也是用array,所以也受10个的限制...
回复

使用道具 举报

🔗
runningMajia 2017-5-9 08:46:50 | 只看该作者
全局:
ecneralc 发表于 2017-5-9 08:45
我提过这个方法,面试官提示说HashMap的实现也是用array,所以也受10个的限制...

面试官的hint是啥?必须要用多层嵌套吗
a家题目太麻烦了也
回复

使用道具 举报

🔗
hamly456 2017-7-30 09:57:31 | 只看该作者
本楼:
全局:
好麻烦。。。
回复

使用道具 举报

🔗
vancexu 2017-8-8 12:50:51 | 只看该作者
全局:
简单点儿方法可以把每个array最后一个item存下一个array的指针,这样每个size为10的array可以存9个元素。在空间上没有很高效,但是实现起来容易一些。
我的意思如下:

  1. class Main {
  2.     static final int LIMIT = 5;
  3.     List<Object> list = new ArrayList<>();
  4.     int head = 0;
  5.     int tail = 0;
  6.     int count = 0;

  7.     public void enqueue(Integer item) {
  8.         if (tail == LIMIT - 1) {
  9.             list.add(new ArrayList<Object>());
  10.             List<Object> newList = (List<Object>) list.get(LIMIT - 1);
  11.             newList.add(item);
  12.             tail = 0;
  13.         } else {
  14.             list.add(item);
  15.         }
  16.         tail++;
  17.         count++;
  18.     }

  19.     public Integer peek() {
  20.         if (count == 0) {
  21.             return null;
  22.         } else {
  23.             return (Integer) list.get(head);
  24.         }
  25.     }

  26.     public Integer pop() {
  27.         if (count == 0) {
  28.             return null;
  29.         } else {
  30.             Integer res;
  31.             if (head == LIMIT - 1) {
  32.                 List<Object> tmp =  (List<Object>) list.get(head);
  33.                 list.clear();
  34.                 list = tmp;
  35.                 head = 0;
  36.             }
  37.             res = (Integer) list.get(head);
  38.             head++;
  39.             count--;
  40.             return res;
  41.         }
  42.     }

  43.     public Integer size() {
  44.         return count;
  45.     }

  46.     public void test() {
  47.         Main m = new Main();
  48.         m.enqueue(1);
  49.         m.enqueue(2);
  50.         m.enqueue(3);
  51.         System.out.println(m.peek());
  52.         System.out.println(m.pop());
  53.         System.out.println(m.peek());
  54.         System.out.println(m.pop());
  55.         m.enqueue(4);
  56.         m.enqueue(5);
  57.         m.enqueue(6);
  58.         m.enqueue(7);
  59.         m.enqueue(8);
  60.         m.enqueue(9);
  61.         System.out.println(m.peek());
  62.         System.out.println(m.pop());
  63.         System.out.println(m.peek());
  64.         System.out.println(m.pop());

  65.     }



  66.     public static void main(String[] args) {
  67.         Main test = new Main();
  68.         test.test();
  69.     }
  70. }
复制代码

评分

参与人数 1大米 +5 收起 理由
高渐离击筑高歌 + 5 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
CalMushroom 2017-8-18 12:40:38 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
better1016 2018-9-24 05:57:09 | 只看该作者
全局:
用linkedinlist 是比较清晰的了吧,试着写了写,有不对的,欢迎提意见
  1. public class QueueWithFixedSizeArray {
  2.        int fixedSize;
  3.        int total;
  4.        class ListNode{
  5.             ListNode next;
  6.             int[] arr;
  7.             int index;
  8.             public ListNode(int size){
  9.                 arr = new int[size];
  10.                 index = 0;
  11.            }
  12.        }
  13.        ListNode head;
  14.        ListNode tail;
  15.        public QueueWithFixedSizeArray(int fixedSize)
  16.        {
  17.            this.fixedSize = fixedSize;
  18.            total = 0;
  19.            head = new ListNode(fixedSize);
  20.            tail = head;
  21.        }

  22.        public void offer(int x)
  23.        {
  24.            // add to the tail
  25.            if(tail.index ==5)
  26.            {
  27.                ListNode next = new ListNode(fixedSize);
  28.                next.arr[next.index] = x;
  29.                next.index++;
  30.                tail.next = next;
  31.                tail = next;
  32.            }
  33.            else
  34.                tail.arr[tail.index++] = x;
  35.        }

  36.        public int  poll()
  37.        {
  38.            if( head.index == 0)
  39.            {
  40.                if(head.next != null && tail != head) {
  41.                    head = head.next;
  42.                }
  43.                else
  44.                     return -1; //not find
  45.            }
  46.            int res = head.arr[0];
  47.            System.arraycopy(head.arr, 1, head.arr, 0, head.arr.length-1);
  48.            head.index--;
  49.            return res;
  50.        }
复制代码
回复

使用道具 举报

🔗
恨天风云 2018-9-28 17:04:36 | 只看该作者
全局:
better1016 发表于 2018-9-24 05:57
用linkedinlist 是比较清晰的了吧,试着写了写,有不对的,欢迎提意见
[mw_shl_code=bash,true]public cla ...

没啥问题貌似,其实网上查一下queue的标准也是类似的结果,都是linkedlist
回复

使用道具 举报

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

本版积分规则

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