楼主: zhaogongzuo
跳转到指定楼层
上一主题 下一主题
收起左侧

g 最新7月电面

🔗
msg7086 2014-7-30 07:23:13 | 只看该作者
全局:
字符串编码的话有现成的方案。
BitTorrent规范里用到的BEncoding就是,字符串长度+冒号+字符串。
直接照着BEncoding的规范做List<String>序列化和反序列化就行了。
回复

使用道具 举报

🔗
arthur1026 2014-8-2 13:41:46 | 只看该作者
全局:
不错,和leetcode上的题风格不同
回复

使用道具 举报

🔗
blesscol 2014-8-2 14:30:04 | 只看该作者
全局:
Google从啥时候开始电面变成这种风格了
回复

使用道具 举报

🔗
loveinCA 2014-8-4 21:59:35 | 只看该作者
全局:
感觉难度还可以
回复

使用道具 举报

🔗
Interviwer 2014-9-17 00:26:35 | 只看该作者
全局:
用 c++ 写了一个, 不是特别简练, 希望指正
  1. #include<iostream>
  2. #include<string>
  3. #include<vector>
  4. using namespace std;

  5. class bitTorrent {

  6. public:
  7.     vector<string> words;
  8.     string benCoding;

  9.     bitTorrent(vector<string> w) {
  10.         words = w;
  11.         benCoding = "";
  12.     }

  13.     void encoding() {
  14.         for(int i = 0; i < words.size(); i ++) {
  15.             benCoding += num2string(words[i].size()) + ":" + words[i];
  16.         }
  17.     }

  18.     vector<string> decoding() {
  19.         vector<string> rst;
  20.         int i = 0;
  21.         while( i < benCoding.size() ) {
  22.             int len = 0;
  23.             while(benCoding[i] >= '0' && benCoding[i] <= '9') {
  24.                 len = 10 * len + ( benCoding[i] - '0' );
  25.                 i ++;
  26.             }
  27.             if(benCoding[i] == ':') {
  28.                 i ++;
  29.                 string oneWord = "";
  30.                 for(int l = i; l < len+i; l ++) {
  31.                     oneWord += benCoding[l];
  32.                 }
  33.                 i += len;
  34.                 rst.push_back(oneWord);
  35.             }else {
  36.                 cout << "Error" << endl;
  37.                 exit(0);
  38.             }
  39.         }
  40.         return rst;
  41.     }
  42.    
  43.     string num2string(int num) {
  44.         string rst = "";
  45.         int base = 1;
  46.         int tmp = num;
  47.         while(tmp != 0) {
  48.             base *= 10;
  49.             tmp /= 10;
  50.         }
  51.         while(base != 1) {
  52.             rst += (num % base + '0');
  53.             base /= 10;
  54.         }
  55.         return rst;
  56.     }

  57. };


  58. int main() {

  59.     vector<string> t;
  60.     t.push_back("hello");
  61.     t.push_back("test");
  62.     t.push_back("10:10");
  63.     t.push_back(":");
  64.     t.push_back("10");


  65.     bitTorrent* test = new bitTorrent(t);
  66.     test->encoding();

  67.     cout << test->benCoding << endl;
  68.    
  69.     vector<string> out = test->decoding();   
  70.     for(int i = 0 ; i < out.size(); i ++) {
  71.         cout << out[i] << endl;
  72.     }

  73. }
复制代码
回复

使用道具 举报

🔗
IamForrest 2014-11-15 00:17:59 | 只看该作者
全局:
Interviwer 发表于 2014-9-17 00:26
用 c++ 写了一个, 不是特别简练, 希望指正

谢谢楼上的c++代码,有几个建议,没必要numtostring,直接用c++11里的to_string就可以了。此外decoding时候每次都push_back(word),这样有大量没必要的copy,建议灵活使用move来消除没必要的损耗,push_back(std::move(word))
回复

使用道具 举报

🔗
GTea 2014-11-15 18:20:30 | 只看该作者
全局:
joy9088 发表于 2014-7-26 04:22
也就是说用"\"作为转义字符

感觉这个办法是对的,连个,一个escape char,可以用比方说'\',一个separator,比方说'|'
回复

使用道具 举报

🔗
jiebour 2015-7-28 13:34:33 | 只看该作者
全局:
jfwwlong 发表于 2014-7-20 10:55
我找了一下,看到一个不错的答案。就是先将每个单词里面的反斜杠'\'加倍变成连续2个'\',然后将每个每个单 ...

Hi 这位仁兄,
为什么你强调了逗号?
thanks!
回复

使用道具 举报

🔗
joseph5wu 2015-7-28 18:10:08 | 只看该作者
全局:
jiebour 发表于 2015-7-28 13:34
Hi 这位仁兄,
为什么你强调了逗号?
thanks!

因为使用逗号来隔开list里面不同的string,这样用来区分这些string里面自带的逗号
回复

使用道具 举报

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

本版积分规则

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