📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
12
返回列表 发新帖
楼主: catfiona
跳转到指定楼层
上一主题 下一主题
收起左侧

UBER电面

🔗
limingli1991 2015-9-15 10:11:21 | 只看该作者
全局:

你好楼主请问一下Median of medians不是只能估计出median么?  这道题不需要算出准确的吗?
回复

使用道具 举报

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

使用道具 举报

🔗
bobzhang2004 2016-2-14 05:05:15 | 只看该作者
全局:
写了下第一题
  1. public class MedianOccurence {

  2.         static class Ele {
  3.                 char c ;
  4.                 int times;
  5.                 public Ele(char c, int t) {
  6.                         this.c = c;
  7.                         times = t;
  8.                 }
  9.         }
  10.        
  11.         public static List<Character> getMedianOccurenceCharactr(char[] chars) {
  12.                 List<Character> res = new ArrayList<Character>();
  13.                 if (chars == null || chars.length == 0) {
  14.                         return res;
  15.                 }
  16.                 HashMap<Character, Integer> map = new HashMap<Character, Integer>();
  17.                 for (char c : chars) {
  18.                         if (map.containsKey(c)) {
  19.                                 map.put(c, map.get(c) + 1);
  20.                         } else {
  21.                                 map.put(c, 1);
  22.                         }
  23.                 }
  24.                 Ele[] eles = new Ele[map.size()];
  25.                 int index = 0;
  26.                 for (Entry<Character, Integer> entry : map.entrySet()) {
  27.                         eles[index++] = new Ele(entry.getKey(), entry.getValue());
  28.                 }
  29.                 int median = getMedian(eles, 0, eles.length - 1);
  30.                 for (Ele ele : eles) {
  31.                         if (ele.times == median) {
  32.                                 res.add(ele.c);
  33.                         }
  34.                 }
  35.                
  36.                 return res;
  37.         }
  38.         private static int getMedian(Ele[] eles, int start, int end) {
  39.                 int k = (eles.length - 1) / 2;
  40.                 int index = parition(eles, start, end);
  41.                 if (index == k) {
  42.                         return eles[index].times;
  43.                 } else if (index > k) {
  44.                         return getMedian(eles, start, index - 1);
  45.                 } else {
  46.                         return getMedian(eles, index + 1, end);
  47.                 }
  48.         }
  49.         private static int parition(Ele[] eles, int start, int end) {
  50.                 int pivot = start;
  51.                 int i = start;
  52.                 int j = start + 1;
  53.                 while (j <= end) {
  54.                         if (eles[j].times < eles[pivot].times) {
  55.                                 i++;
  56.                                 swap(eles, i, j);
  57.                         }
  58.                         j++;
  59.                 }
  60.                 swap(eles, i, pivot);
  61.                 return i;
  62.         }
  63.         private static void swap(Ele[] eles, int i, int j) {
  64.                 Ele tmp = eles[i];
  65.                 eles[i] = eles[j];
  66.                 eles[j] = tmp;
  67.         }
  68.        
  69.         public static void main(String[] args) {
  70.                 char[] chars = {'a', 'b', 'b','c', 'c', 'c', 'e', 'e', 'e', 'd', 'd', 'd', 'd'};
  71.                 List<Character> res = getMedianOccurenceCharactr(chars);
  72.                 for (char c : res) {
  73.                         System.out.println(c);
  74.                 }
  75.         }
  76.        
  77. }
复制代码
回复

使用道具 举报

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

本版积分规则

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