📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
楼主: mm豆
跳转到指定楼层
上一主题 下一主题
收起左侧

google二面

🔗
bobzhang2004 2015-12-2 13:04:39 | 只看该作者
全局:
补全了楼主的代码,for (int i = start + 1; i <= end; i++) 楼主代码有个小bug,这里应该是要有等于的
  1. // 这个的输出是没有顺序的
  2. public class TopKWordsII {
  3.        
  4.         static class Word {
  5.                 String val;
  6.                 int num;
  7.                 public Word(String val, int num) {
  8.                         this.num = num;
  9.                         this.val = val;
  10.                 }
  11.         }
  12.         public static void main(String[] args) {
  13.                 Word[] array = {new Word("A",400), new Word("B",100), new Word("C",500), new Word("D",200), new Word("E",300)};
  14.                 getPopularQueries(array, 1000);
  15.         }
  16.         public static List<Word> getPopularQueries(Word[] array, int target) {
  17.                 List<Word> ret = selection(array, target, 0, array.length - 1);
  18.                 for (Word i : ret) {
  19.                         System.out.println(i.val);
  20.                 }
  21.                 return ret;
  22.         }

  23.         private static List<Word> selection(Word[] array, int target, int start,
  24.                         int end) {
  25.                 if (start == end) {
  26.                         return getRet(array, 0, start);
  27.                 }
  28.                 int p = new Random().nextInt(end - start) + start;
  29.                 int pivot = array[p].num;
  30.                 swap(array, start, p);
  31.                 int pre = start;
  32.                 for (int i = start + 1; i <= end; i++) {
  33.                         if (array[i].num > pivot) {
  34.                                 swap(array, ++pre, i);
  35.                         }
  36.                 }
  37.                 swap(array, start, pre);
  38.                 int sum = getSum(array, start, pre);
  39.                 if (sum == target) {
  40.                         return getRet(array, 0, pre);
  41.                 } else if (sum < target) {
  42.                         return selection(array, target - sum, pre + 1, end);
  43.                 } else {
  44.                         return selection(array, target, start, pre);
  45.                 }
  46.         }

  47.         private static List<Word> getRet(Word[] array, int start, int end) {
  48.                 List<Word> res = new ArrayList<Word>();
  49.                 for (int i = start; i <= end; i++) {
  50.                         res.add(array[i]);
  51.                 }
  52.                 return res;
  53.         }

  54.         private static int getSum(Word[] array, int start, int pre) {
  55.                 int sum = 0;
  56.                 for (int i = start; i <= pre; i++) {
  57.                         sum += array[i].num;
  58.                 }
  59.                 return sum;
  60.         }

  61.         private static void swap(Word[] array, int i, int j) {
  62.                 Word tmp = array[i];
  63.                 array[i] = array[j];
  64.                 array[j] = tmp;
  65.         }
  66. }
复制代码
回复

使用道具 举报

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

本版积分规则

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