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

Google Onsite

🔗
一路向北~ 2016-1-14 15:17:46 | 只看该作者
全局:
楼主有结果了吗?
回复

使用道具 举报

🔗
bobzhang2004 2016-1-25 06:30:56 | 只看该作者
全局:
kinggarden2001 发表于 2015-12-28 01:52
leetcode昨天刚加了这个题 用dp算最少个数 同时cache每步的最优解 最后应该能同时得出最少个数以及是哪些硬 ...

怎么cache呢?用hashMap吗?
回复

使用道具 举报

🔗
bobzhang2004 2016-1-25 06:33:40 | 只看该作者
全局:
bounded blocking queue应该还需要是add什么东西吧?不能只有时间吧?
回复

使用道具 举报

🔗
bobzhang2004 2016-1-25 07:02:02 | 只看该作者
全局:
写了下打印树的题,最后输出格式还是有一些差异
  1. public class PrintStructrue {

  2.         public static void main(String[] args) {
  3.                 PrintStructrue ps = new PrintStructrue();
  4.                 int[][] edges = { { 7, 6 }, { 7, 5 }, { 7, 4 }, { 6, 3 }, { 6, 2 } };
  5.                 ps.printStructure(edges);
  6.         }

  7.         public void printStructure(int[][] edges) {
  8.                 if (edges == null || edges.length == 0) {
  9.                         return;
  10.                 }
  11.                 HashMap<Integer, HashSet<Integer>> graph = buildGraph(edges);
  12.                 HashMap<Integer, Integer> counter = new HashMap<Integer, Integer>();
  13.                 for (HashSet<Integer> set : graph.values()) {
  14.                         for (int ele : set) {
  15.                                 if (counter.containsKey(ele)) {
  16.                                         counter.put(ele, counter.get(ele) + 1);
  17.                                 } else {
  18.                                         counter.put(ele, 1);
  19.                                 }
  20.                         }
  21.                 }
  22.                 Queue<Integer> queue = new LinkedList<Integer>();
  23.                 for (int ele : graph.keySet()) {
  24.                         if (!counter.containsKey(ele)) {
  25.                                 queue.offer(ele);
  26.                         }
  27.                 }
  28.                 while (!queue.isEmpty()) {
  29.                         int val = queue.poll();
  30.                         print(val, graph, 0);
  31.                 }
  32.         }

  33.         private void print(int val, HashMap<Integer, HashSet<Integer>> graph,
  34.                         int len) {
  35.                 for (int i = 0; i < len; i++) {
  36.                         if (i == 0) {
  37.                                 System.out.print(" ");
  38.                         }
  39.                         System.out.print("-");
  40.                 }
  41.                 System.out.println(val);
  42.                 for (int i : graph.get(val)) {
  43.                         print(i, graph, len + 1);
  44.                 }
  45.         }

  46.         private HashMap<Integer, HashSet<Integer>> buildGraph(int[][] edges) {
  47.                 HashMap<Integer, HashSet<Integer>> map = new HashMap<Integer, HashSet<Integer>>();
  48.                 for (int[] edge : edges) {
  49.                         if (!map.containsKey(edge[0])) {
  50.                                 map.put(edge[0], new HashSet<Integer>());
  51.                         }
  52.                         if (!map.containsKey(edge[1])) {
  53.                                 map.put(edge[1], new HashSet<Integer>());
  54.                         }
  55.                         map.get(edge[0]).add(edge[1]);
  56.                 }
  57.                 return map;
  58.         }
  59. }
复制代码
回复

使用道具 举报

🔗
ninacc 2016-2-22 06:46:33 | 只看该作者
全局:
bobzhang2004 发表于 2016-1-24 18:02
写了下打印树的题,最后输出格式还是有一些差异

你好,请问这一题的题目是什么意思能和我说下吗,看不明白,谢谢!
回复

使用道具 举报

🔗
bobzhang2004 2016-3-5 23:48:37 | 只看该作者
全局:
第五轮是找硬币题,不过是打印出最少硬币的序列.这个题是要怎么存序列呢?hashmap还是stack
回复

使用道具 举报

🔗
bobzhang2004 2016-3-6 00:30:25 | 只看该作者
全局:
coin题,假设每个硬币只能用一次
  1. public class CoinsChangeIII {

  2.         public static void main(String[] args) {
  3.                 int[] coins = { 1, 2, 3, 4, 5, 6, 10 };
  4.                 List<Integer> res = minChange(coins, 9);
  5.                 for (int i : res) {
  6.                         System.out.print(i + " ");
  7.                 }
  8.         }

  9.         public static List<Integer> minChange(int[] coins, int num) {
  10.                 int[] table = new int[num + 1];
  11.                 int[] solution = new int[num + 1];
  12.                 Arrays.fill(table, Integer.MAX_VALUE);
  13.                 table[0] = 0;
  14.                 for (int i = 0; i < coins.length; i++) {
  15.                         for (int j = num; j > 0; j--) {
  16.                                 if (j >= coins[i] && table[j - coins[i]] != Integer.MAX_VALUE ) {
  17.                                         if (table[j - coins[i]] + 1 < table[j]) {
  18.                                                 table[j] = table[j - coins[i]] + 1;
  19.                                                 solution[j] = i;
  20.                                         }
  21.                                 }
  22.                         }
  23.                 }
  24.                
  25.                 List<Integer> res = new ArrayList<Integer>();
  26.                 if (table[num] == Integer.MAX_VALUE) {
  27.                         return res;
  28.                 }
  29.                 while (num > 0) {
  30.                         res.add(coins[solution[num]]);
  31.                         num -= coins[solution[num]];
  32.                 }
  33.                 Collections.reverse(res);
  34.                 return res;

  35.         }
  36. }
复制代码
回复

使用道具 举报

🔗
bobzhang2004 2016-3-6 00:30:50 | 只看该作者
全局:
coin题,假设每个硬币可以用多次
  1. public class CoinsChangeII {

  2.         public static void main(String[] args) {
  3.                 int[] coins = { 1, 4, 5, 10 };
  4.                 List<Integer> res = minChange(coins, 8);
  5.                 for (int i : res) {
  6.                         System.out.print(i + " ");
  7.                 }
  8.         }

  9.         public static List<Integer> minChange(int[] coins, int num) {
  10.                 int[] table = new int[num + 1];
  11.                 int[] solution = new int[num + 1];
  12.                 for (int i = 1; i <= num; i++) {
  13.                         int minNum = Integer.MAX_VALUE;
  14.                         int minIndex = Integer.MAX_VALUE;
  15.                         for (int j = 0; j < coins.length; j++) {
  16.                                 if (coins[j] <= i && 1 + table[i - coins[j]] < minNum) {
  17.                                         minNum = 1 + table[i - coins[j]];
  18.                                         minIndex = j;
  19.                                 }
  20.                         }
  21.                         table[i] = minNum;
  22.                         solution[i] = minIndex;
  23.                 }

  24.                 List<Integer> res = new ArrayList<Integer>();
  25.                 if (table[num] == Integer.MAX_VALUE) {
  26.                         return res;
  27.                 }
  28.                 while (num > 0) {
  29.                         res.add(coins[solution[num]]);
  30.                         num -= coins[solution[num]];
  31.                 }

  32.                 return res;

  33.         }
  34. }
复制代码
回复

使用道具 举报

🔗
jy_121 2016-5-1 02:54:24 | 只看该作者
全局:
硬币题我是dp时每个点都用一个list来保存路径

补充内容 (2016-5-1 02:55):
不知道面试官希望的是什么样子的?
回复

使用道具 举报

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

本版积分规则

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