📣 黑五网一特惠|VIP一年通行证5折,蓝莓、Offer多多同步特惠
回复: 4
跳转到指定楼层
上一主题 下一主题
收起左侧

24点

全局:

2016(1-3月) 码农类General 硕士 全职@google - 内推 - HR筛选  | | Other | 其他

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
一道面经中经常出现的题
coding一道 类似24点。
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
个是要用recursion来枚举吗?

上一篇:Quora 在线test
下一篇:来一個g电面面经吧。
推荐
adiggo 2016-4-3 02:27:50 | 只看该作者
全局:
所有数字必须用到么
回复

使用道具 举报

🔗
 楼主| bobzhang2004 2016-4-3 01:54:05 | 只看该作者
全局:
  1. public class TwentyFour {

  2.         public static void main(String[] args) {
  3.                 TwentyFour tf = new TwentyFour();
  4.                 int[] arr = { 1, 50, 3, 6, 7 };
  5.                 System.out.println(tf.canGetTwentyFour(arr, 60));
  6.         }

  7.         public boolean canGetTwentyFour(int[] arr, int target) {
  8.                 if (arr == null || arr.length == 0) {
  9.                         return false;
  10.                 }
  11.                 Arrays.sort(arr);
  12.                 boolean[] visited = new boolean[arr.length];
  13.                 List<List<Integer>> lists = new ArrayList<List<Integer>>();
  14.                 getPermutation(lists, new ArrayList<Integer>(), arr, visited);

  15.                 for (List<Integer> candidate : lists) {
  16.                         List<Integer> list = helper(candidate, target, 0, arr.length - 1);
  17.                         for (int i : list) {
  18.                                 if (i == target) {
  19.                                         return true;
  20.                                 }
  21.                         }
  22.                 }
  23.                 return false;
  24.         }

  25.         private void getPermutation(List<List<Integer>> lists,
  26.                         ArrayList<Integer> list, int[] arr, boolean[] visited) {
  27.                 if (list.size() == arr.length) {
  28.                         lists.add(new ArrayList<Integer>(list));
  29.                         return;
  30.                 }
  31.                 for (int i = 0; i < arr.length; i++) {
  32.                         if (visited[i] || i > 0 && arr[i] == arr[i - 1] && !visited[i - 1]) {
  33.                                 continue;
  34.                         }
  35.                         visited[i] = true;
  36.                         list.add(arr[i]);
  37.                         getPermutation(lists, list, arr, visited);
  38.                         list.remove(list.size() - 1);
  39.                         visited[i] = false;
  40.                 }
  41.         }

  42.         private List<Integer> helper(List<Integer> arr, int target, int start,
  43.                         int end) {
  44.                 List<Integer> res = new ArrayList<Integer>();
  45.                 if (start > end) {
  46.                         return res;
  47.                 }
  48.                 if (start == end) {
  49.                         res.add(arr.get(start));
  50.                         return res;
  51.                 }

  52.                 for (int i = start; i < end; i++) {
  53.                         List<Integer> left = helper(arr, target, start, i);
  54.                         List<Integer> right = helper(arr, target, i + 1, end);
  55.                         for (int l : left) {
  56.                                 for (int r : right) {
  57.                                         res.add((l + r));
  58.                                         res.add((l * r));
  59.                                         if (r != 0) {
  60.                                                 res.add((l / r));
  61.                                         }
  62.                                         res.add((l - r));
  63.                                 }
  64.                         }
  65.                 }
  66.                 return res;
  67.         }
  68. }
复制代码
回复

使用道具 举报

🔗
 楼主| bobzhang2004 2016-4-3 02:31:06 | 只看该作者
全局:
adiggo 发表于 2016-4-3 02:27
所有数字必须用到么

应该是的,并且顺序不一定
回复

使用道具 举报

🔗
jy_121 2016-4-27 05:21:39 | 只看该作者
全局:
除了枚举出所有情况还有其他做法吗?
回复

使用道具 举报

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

本版积分规则

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