📣 Back to School开学季 - VIP通行证5折优惠!蓝莓、Offer多多同步优惠
楼主: bearcat001
跳转到指定楼层
上一主题 下一主题
收起左侧

Google 11月19号 实习面经 和 12月18号 旁观面经

🔗
bobzhang2004 2016-1-27 09:23:15 | 只看该作者
全局:
写了下第一轮第二问,楼主可以详细讲讲第三问怎么做吗?
  1. public class Straight {
  2.        
  3.         static class Point {
  4.                 int val;
  5.                 int count;
  6.                
  7.                 public Point(int val, int count) {
  8.                         this.val = val;
  9.                         this.count = count;
  10.                 }
  11.         }
  12.         public static boolean isExactXStraight(int[] nums, int x) {
  13.                 if (nums == null || nums.length < x) {
  14.                         return false;
  15.                 }
  16.                 PriorityQueue<Point> pq = new PriorityQueue<Point>(11, new Comparator<Point>() {
  17.                         public int compare(Point p1, Point p2) {
  18.                                 return p1.val - p2.val;
  19.                         }
  20.                 });
  21.                 HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
  22.                 for (int i : nums) {
  23.                         if (map.containsKey(i)) {
  24.                                 map.put(i, map.get(i) + 1);
  25.                         } else {
  26.                                 map.put(i, 1);
  27.                         }
  28.                 }
  29.                 for (int key : map.keySet()) {
  30.                         pq.offer(new Point(key, map.get(key)));
  31.                 }
  32.                 while (!pq.isEmpty()) {
  33.                         List<Point> list = new ArrayList<Point>();
  34.                         Point prev = pq.poll();
  35.                         list.add(prev);
  36.                         for (int i = 1; i < x; i++) {
  37.                                 if (pq.isEmpty()) {
  38.                                         return false;
  39.                                 }
  40.                                 Point cur = pq.poll();
  41.                                 list.add(cur);
  42.                                 if (cur.val != prev.val + 1) {
  43.                                         return false;
  44.                                 }
  45.                                 prev = cur;
  46.                         }
  47.                         for (Point p : list) {
  48.                                 p.count = p.count - 1;
  49.                                 if (p.count != 0) {
  50.                                         pq.offer(p);
  51.                                 }
  52.                         }
  53.                 }
  54.                
  55.                 return true;
  56.         }
  57.        
  58.         public static void main(String[] args) {
  59.                 int[] nums = {1, 2, 3, 4, 4, 5, 5, 6, 7, 8};
  60.                 boolean res = isExactXStraight(nums, 5);
  61.                 System.out.println(res);
  62.         }
  63. }
复制代码
回复

使用道具 举报

🔗
 楼主| bearcat001 2016-1-27 09:29:29 | 只看该作者
全局:

这儿是我的解,不过是用 C++ 写的
回复

使用道具 举报

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

本版积分规则

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