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

Google 电面

🔗
 楼主| jpeng7 2016-11-30 07:40:38 | 只看该作者
全局:
catinclay 发表于 2016-11-30 07:37
恩 先combination再permutation, 挺麻烦的 以为全部的button都要用到

补充内容 (2016-11-30 07:38):

我用backtracking求的combination,你的意思是?
回复

使用道具 举报

🔗
catinclay 2016-11-30 07:51:41 | 只看该作者
全局:
jpeng7 发表于 2016-11-30 07:40
我用backtracking求的combination,你的意思是?

其实应该都差不多啦...
no more idea
回复

使用道具 举报

🔗
zhangxi1994 2016-11-30 11:22:54 | 只看该作者
全局:
第一题file那个不太明白是什么意思诶,,楼主能再详细说一下问题么?为什么是一行一行的存啊。。
回复

使用道具 举报

🔗
 楼主| jpeng7 2016-11-30 12:13:59 | 只看该作者
全局:
zhangxi1994 发表于 2016-11-30 11:22
第一题file那个不太明白是什么意思诶,,楼主能再详细说一下问题么?为什么是一行一行的存啊。。

有一个input file,内容是
apple pen
pineapple pen
ang
penpineapple
apple pen
其中第一行跟最后一行是一样的,所以只需要输出第一行。不用care input file这个条件,可以只当是一个string数组
回复

使用道具 举报

🔗
zhangxi1994 2016-12-1 05:26:09 | 只看该作者
全局:
jpeng7 发表于 2016-11-30 12:13
有一个input file,内容是
apple pen
pineapple pen

谢谢楼主!想再问一下,一个map的size比较小那道题,是问怎么实现hashmap吗?
回复

使用道具 举报

🔗
 楼主| jpeng7 2016-12-1 07:13:54 | 只看该作者
全局:
zhangxi1994 发表于 2016-12-1 05:26
谢谢楼主!想再问一下,一个map的size比较小那道题,是问怎么实现hashmap吗?

对的,紫薯紫薯紫薯
回复

使用道具 举报

🔗
xxxxx56789 2016-12-2 08:33:21 | 只看该作者
全局:
        第二题这样写不知道对不对

        public static  List<String>  buttonCombine(List<Integer> nums){
             List<String> re = new ArrayList<String>();
             boolean[] used = new boolean[nums.size()];
             help(re,0, new StringBuilder(), used, nums);
             return re;
        }
       
        public static void help( List<String> r,int level, StringBuilder sb, boolean[] used, List<Integer> nums){
            int size = nums.size();
            if(sb.length()!=0 && sb.charAt(sb.length()-1)!='-'){
                    StringBuilder newsb = new StringBuilder(sb);
                    r.add(newsb.toString());
            }
                if(level == size) return;
            for(int i=0; i<size; i++){
                        if(!used[i]){
                                used[i] = true;
                                sb.append(nums.get(i));
                                help(r,level+1, sb, used, nums);
                                sb.deleteCharAt(sb.length()-1);
                                if(level!=size-1){
                                        sb.append(nums.get(i)+"-");
                                        help(r,level+1, sb, used, nums);
                                        sb.delete(sb.length()-2, sb.length());
                                }
                                used[i] = false;
                        }
                }
        }
回复

使用道具 举报

🔗
阿童木 2016-12-2 08:45:25 | 只看该作者
全局:
jpeng7 发表于 2016-11-30 15:13
对的,紫薯紫薯紫薯

hashmap的那个题楼主现在知道怎么做了吗?
回复

使用道具 举报

🔗
sumomoshinqi 2016-12-2 10:27:17 | 只看该作者
全局:
按按钮,先组合,再全排,然后插 '-'
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;

  5. void CombButtons(string& buttons, int l, int r, int len,
  6.                  vector<string>& button_select, string path) {
  7.   if (path.size() == len)
  8.     button_select.push_back(path);
  9.   else {
  10.     for (int i = l; i <= r; i++) {
  11.       path += buttons[i];
  12.       CombButtons(buttons, i + 1, r, len, button_select, path);
  13.       path.erase(path.end() - 1);
  14.     }
  15.   }
  16. }

  17. void PermButtons(string& buttons, int l, int r, vector<string>& button_seq) {
  18.   if (l == r)
  19.     button_seq.push_back(buttons);
  20.   else {
  21.     for (int i = l; i <= r; i++) {
  22.       swap(buttons[i], buttons[l]);
  23.       PermButtons(buttons, l + 1, r, button_seq);
  24.       swap(buttons[i], buttons[l]);
  25.     }
  26.   }
  27. }

  28. void PressButtons(string buttons, int i, int n, vector<string>& res,
  29.                   string path) {
  30.   if (i == n)
  31.     res.push_back(path);
  32.   else {
  33.     path += buttons[i];
  34.     PressButtons(buttons, i + 1, n, res, path);
  35.     if (i + 1 < n) PressButtons(buttons, i + 1, n, res, path + '-');
  36.   }
  37. }

  38. int main(int argc, char const* argv[]) {
  39.   string buttons = "123";
  40.   int n = buttons.size();
  41.   vector<string> res;
  42.   vector<string> button_seq;
  43.   vector<string> button_select;
  44.   for (int len = 1; len <= n; len++)
  45.     CombButtons(buttons, 0, n - 1, len, button_select, "");
  46.   for (auto bs : button_select) PermButtons(bs, 0, bs.size() - 1, button_seq);
  47.   for (auto pb : button_seq) PressButtons(pb, 0, pb.size(), res, "");
  48.   for (auto r : res) cout << r << endl;
  49.   return 0;
  50. }
复制代码
回复

使用道具 举报

🔗
onlinecco 2017-1-14 15:31:54 | 只看该作者
全局:
sumomoshinqi 发表于 2016-12-2 10:27
按按钮,先组合,再全排,然后插 '-'

PermButtons写错了吧。。这样会少很多permutation的

补充内容 (2017-1-14 15:34):
看错了 看成i+1了
回复

使用道具 举报

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

本版积分规则

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