📣 独立日限时特惠: VIP通行证立减$68
楼主: Mr.111
跳转到指定楼层
上一主题 下一主题
收起左侧

Google OA 试题讨论

🔗
huai10 2016-8-23 10:46:54 | 只看该作者
全局:
Mr.111 发表于 2016-8-23 05:52
电面也需要做题吗?大神一共面试了几轮?希望大神和大家分享一下经验

OA, 然后一轮电面, 然后onsite
回复

使用道具 举报

🔗
 楼主| Mr.111 2016-8-23 11:06:44 | 只看该作者
全局:
huai10 发表于 2016-8-23 10:46
OA, 然后一轮电面, 然后onsite

好的,谢谢分享!
回复

使用道具 举报

🔗
 楼主| Mr.111 2016-8-23 13:00:00 | 只看该作者
全局:
本帖最后由 Mr.111 于 2016-8-23 13:04 编辑

根据@stellari 大神的指点,今天花时间研究了一下,然后自己写了一个C#版本。目前测试一切正常,代码如下,希望和大家共同进步,如果有不正确的地方真心求正
  1. public int solution(String S)
  2. {
  3.     string[] input = S.Split('\n');

  4.     string[] isImage = { ".jpeg", ".gif", ".png" };
  5.     int rezult = 0;
  6.     Stack<int> holder = new Stack<int>();

  7.     for (int i = 0; i < input.Length; i++)
  8.     {
  9.         string currentNode = input[i].Replace(" ", "");
  10.         int currentLength = currentNode.Length; //The length with the "/"
  11.         int currentLevel = input[i].Length - currentLength; //Find the level
  12.         
  13.         while (currentLevel < holder.Count) //Find the full path length of current node's last parent
  14.         {
  15.             holder.Pop();
  16.         }

  17.         if (holder.Any()) //Add the full path length of current node, if the node is not in the 1 level
  18.         {
  19.             holder.Push(holder.Peek() + currentLength + 1);
  20.         }
  21.         else //Add the full path length of current node, if the node is in the 1 level
  22.         {
  23.             holder.Push(currentLength + 1);
  24.         }

  25.         if (isImage.Any(n => currentNode.Contains(n))) //Add the full path length of current node to the rezult, if the current node is image file
  26.         {
  27.             rezult += holder.Peek();
  28.         }
  29.     }

  30.     return rezult;
  31. }
复制代码
回复

使用道具 举报

🔗
619899442 2016-10-7 03:59:18 | 只看该作者
全局:
本帖最后由 619899442 于 2016-10-7 04:00 编辑

写了一个基于递归的parser,求交流。。。
我的想法是:首先把输入转化成一个string list其中每个元素对应一行。在递归调用_build_subtree会对当前节点及其子树进行分析,返回下一个待分析的(对于这棵子树的中间结果,文件名的index)
  1. class Solution:
  2.     dic = {'.jpeg', '.png', '.gif'}

  3.     @staticmethod
  4.     def _is_pic(name):
  5.         for ext in Solution.dic:
  6.             if len(name) > len(ext) and name[-len(ext):] == ext:
  7.                 return True
  8.         return False

  9.     @staticmethod
  10.     def _level(name):
  11.         return len(name) - len(name.strip())

  12.     @staticmethod
  13.     def _build_subtree(strings, start):     # return the (total length, next position)
  14.         root = strings[start]
  15.         this_level = Solution._level(root)
  16.         count = 0
  17.         next_file = start + 1

  18.         # base case: leaf node
  19.         if next_file < len(strings) and Solution._level(strings[next_file]) <= this_level or next_file == len(strings):
  20.             # leaf node
  21.             if Solution._is_pic(root):
  22.                 return 1 + len(root.strip()), next_file
  23.             else:
  24.                 return 0, next_file

  25.         while next_file < len(strings) and Solution._level(strings[next_file]) > this_level:
  26.             tmp, next_file = Solution._build_subtree(strings, next_file)
  27.             if tmp > 0:
  28.                 count += (tmp + len(root.strip()) + 1)
  29.         return count, next_file

  30.     @staticmethod
  31.     def build_tree(string):
  32.         strings = string.split('\n')
  33.         next_file = 0
  34.         res = 0
  35.         while next_file < len(strings):
  36.             tmp, next_file = Solution._build_subtree(strings, next_file)
  37.             res += tmp
  38.         return res



  39. if __name__ == "__main__":
  40.     directory = 'dir1\n dir11\n dir12\n  picture.jpeg\ndir121\n file1.txt\ndir2\n file2.gif'
  41.     print(Solution.build_tree(directory))
复制代码
回复

使用道具 举报

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

本版积分规则

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