123
返回列表 发新帖
楼主: sumingche
跳转到指定楼层
上一主题 下一主题
收起左侧

一道Amazon intern面试题有感

🔗
北美农民 2014-3-30 02:14:40 | 只看该作者
全局:
cqx83 发表于 2014-3-29 03:23
实在没看出这题有用bitset的必要啊。。

just for saving memory.

if you did this I think interviewer would give u a higher feedback.
回复

使用道具 举报

🔗
anikin0617 2014-3-30 07:32:15 | 只看该作者
全局:
北美农民 发表于 2014-3-30 02:09
Sry cannot type Chinese.

This is for going backward and recover bits if we did not find solutio ...

Thank you very much, really resolve my confusion.
回复

使用道具 举报

🔗
Jonson 2014-10-11 20:57:57 | 只看该作者
全局:
本帖最后由 Jonson 于 2014-10-11 20:59 编辑

Longford Pairing 既然是一种permutation, 自然就是考虑用DFS, 在DFS的过程中,每次尝试加入一个新的数字到solution中时, 都要check是否满足Longford Pairing的feature, 所以这道题的思路也是一种backtracking的想法。注意需要一个List<List<Integer>>保存结果。
  1. public class LongfordSequence {
  2.         public List<List<Integer>> gernerateSequence(int n) {
  3.                 List<List<Integer>> result = new ArrayList<List<Integer>>();
  4.                 int[] count = new int[n + 1];
  5.                 gernerateHelper(result, new ArrayList<Integer>(), count);
  6.                 return result;
  7.         }

  8.         private void gernerateHelper(List<List<Integer>> result, List<Integer> solution, int[] count) {
  9.                 if (solution.size() == 2 * (count.length - 1)) {
  10.                         result.add(new ArrayList<Integer>(solution));
  11.                         return;
  12.                 }

  13.                 for (int i = 1; i <= count.length - 1; i++) {
  14.                         if (count[i] == 0) {
  15.                                 solution.add(i);
  16.                                 count[i] = 1;
  17.                                 gernerateHelper(result, solution, count);
  18.                                 solution.remove(solution.size() - 1);
  19.                                 count[i] = 0;
  20.                         } else if (count[i] == 1) {
  21.                                 int size = solution.size();
  22.                                 if (size - i - 1 >= 0 && solution.get(size - i - 1) == i) {
  23.                                         solution.add(i);
  24.                                         count[i] = 2;
  25.                                         gernerateHelper(result, solution, count);
  26.                                         solution.remove(solution.size() - 1);
  27.                                         count[i] = 1;
  28.                                 }
  29.                         }
  30.                 }
  31.         }
  32. }
复制代码
回复

使用道具 举报

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

本版积分规则

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