📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
楼主: qlan2
跳转到指定楼层
上一主题 下一主题
收起左侧

google电面

🔗
yuanb10 2015-10-1 21:36:08 | 只看该作者
本楼:
全局:
IBM好屌丝。。。。。
回复

使用道具 举报

🔗
0011999 2015-10-2 03:36:01 | 只看该作者
全局:
thanks for sharing
回复

使用道具 举报

🔗
hulahu 2015-10-2 05:41:51 | 只看该作者
全局:
感觉第一题用hashmap 做比较好
  1. public static int cal(String input){
  2.                 int countLeft = 0;
  3.                 int countRight = 0;
  4.                 HashMap<Integer, String> map = new HashMap<Integer, String>();
  5.                 int prevLeft = 0;
  6.                 for(int i = 0; i < input.length(); i++){
  7.                         if(input.charAt(i) == '('){
  8.                                
  9.                                 if(i > prevLeft){
  10.                                         String exp = input.substring(prevLeft+1, i);
  11.                                         map.put(countLeft, exp);
  12.                                 }
  13.                                 countLeft++;
  14.                                 prevLeft = i;
  15.                                
  16.                         } else if(input.charAt(i) == ')'){
  17.                                 if(countRight == 0){
  18.                                         String exp = input.substring(prevLeft + 1, i);
  19.                                         map.put(countLeft, exp);
  20.                                         countRight++;
  21.                                 }
  22.                                
  23.                         }
  24.                                
  25.                 }
  26.                
  27.                 return _cal(map, countLeft);
  28.         }
  29.        
  30.         private static int _cal(HashMap<Integer, String> map, int max){
  31.                 Stack<Integer> stack = new Stack<Integer>();
  32.                 while(max >= 1){
  33.                         String exp = map.get(max);
  34.                         if(!stack.isEmpty()){
  35.                                 stack.push(eval(exp, stack.pop()));
  36.                                
  37.                         } else{
  38.                                 stack.push(eval(exp, 0));
  39.                         }
  40.                         max--;
  41.                 }
  42.                 return stack.pop();
  43.         }
  44.        
  45.         private static int eval(String exp, int d){
  46.                 char op = exp.charAt(0);
  47.                
  48.                 int res = 0;
  49.                 if(op == '+'){
  50.                         for(int i = 1; i < exp.length(); i++){
  51.                                 res += exp.charAt(i) - '0';
  52.                         }
  53.                         return res + d;
  54.        
  55.                 }else{
  56.                         res = 1;
  57.                         for(int i = 1; i < exp.length(); i++){
  58.                                 res *= (exp.charAt(i) - '0');
  59.                                

  60.                         }
  61.                         if(d != 0) res *= d;
  62.                         return res;
  63.                 }
  64.                                                
  65.         }
  66.        
复制代码

补充内容 (2015-10-2 05:42):
调了半天, 才弄好。 大家看看, 有没有更好的solution
回复

使用道具 举报

全局:
感觉如果不考虑表达式可能不正确以及digit都是一位不超过10的话好像用stack挺简单的
用Python写了下不知道如何
  1. class Solution:
  2.     def expressParser(self, express):
  3.         if not express: return 0
  4.         stack = []
  5.         for i in range(len(express)):
  6.             if express[i] == ')':
  7.                 nums = []
  8.                 while stack[-1] not in '+*':
  9.                     nums.append(stack.pop())
  10.                 op = stack.pop()
  11.                 temp = self.calculate(op, nums)
  12.                 stack.pop() # pop '('
  13.                 stack.append(temp)
  14.             else:
  15.                 stack.append(express[i])
  16.         return int(stack[0])

  17.     def calculate(self, op, nums):
  18.         if op == '+':
  19.             res = 0
  20.             for n in nums:
  21.                 res += int(n)
  22.         else:
  23.             res = 1
  24.             for n in nums:
  25.                 res *= int(n)
  26.         return str(res)
复制代码
回复

使用道具 举报

🔗
don9uixote 2015-10-4 15:16:46 | 只看该作者
全局:
如果这道题的string是正确的,如果string的形式是像lz列出来一样,每个括号内如果还有括号,内部的括号都靠右的话(即所有的右括号')'都在string的最右端),可以用recursion。
如果右括号不全是在string最右边,也可以recursion,只不过要记住内部左右括号的位置。
  1. public static int parse(String s) {
  2.         if (s == null || s.length() == 0) {
  3.                 return 0;
  4.         }
  5.         return helper(s, 0, s.length() - 1);
  6. }

  7. private static int helper(String s, int lo, int hi) {
  8.         int innerLo = -1;                                // if there is inner parentheses
  9.         char oper = s.charAt(lo + 1);        // the operator: '+' or '*'

  10.         int res = 1;                                        // result of string  from lo ~ hi
  11.         for (int i = lo + 2; i < hi; i++) {
  12.                 char ch = s.charAt(i);

  13.                 if (ch == '(') {
  14.                         innerLo = i;                        // calculate the right part in recursion
  15.                         break;
  16.                 }

  17.                 if (oper == '+') {
  18.                         res += ch - '0';
  19.                 } else if (oper == '*') {
  20.                         res *= ch - '0';
  21.                 }
  22.         }

  23.         if (oper == '+') {
  24.                         return res - 1 + (innerLo < 0 ? 0
  25.                         : helper(s, innerLo, hi - 1));
  26.         }

  27.         return res * (innerLo < 0 ? 1 : helper(s, innerLo, hi - 1));
  28. }
复制代码
回复

使用道具 举报

🔗
readman 2015-10-5 04:35:41 | 只看该作者
全局:
don9uixote 发表于 2015-10-4 15:16
如果这道题的string是正确的,如果string的形式是像lz列出来一样,每个括号内如果还有括号,内部的括号都靠 ...

"(+(+1(+11))1)"~=
回复

使用道具 举报

🔗
don9uixote 2015-10-5 05:03:53 | 只看该作者
全局:
额,我假设的是字符串末尾是连续的‘)’
像你给的这这个string,确定当前部分内部还有括号的话,再从右往左找第二个‘)’..
回复

使用道具 举报

🔗
 楼主| qlan2 2015-10-6 01:28:56 | 只看该作者
全局:
楼主吧自己的代码贴出来方便大家准备google面试,祝大家面试成功!
  1. public static int parser(String str) {
  2.                 int N = str.length(), sum = 0;
  3.                 boolean isFirst = true;
  4.                 char op = '+';
  5.                 Deque<Integer> numStack = new ArrayDeque<>();
  6.                 Deque<Character> opStack = new ArrayDeque<>();
  7.                
  8.                 for (int i = 0; i < N; i++) {
  9.                         char c = str.charAt(i);
  10.                        
  11.                         if (c >= '0' && c <= '9') {
  12.                                 sum = cal(sum, c - '0', op, isFirst);
  13.                                 isFirst = false;
  14.                         }else if (c == '+' || c == '*') {
  15.                                 op = c;
  16.                         }else if (c == '(') {
  17.                                 numStack.push(sum);
  18.                                 sum = 0;
  19.                                 isFirst = true;
  20.                                 opStack.push(op);
  21.                         }else if (c == ')') {
  22.                                 op = opStack.pop();
  23.                                 sum = cal(numStack.pop(), sum, op, isFirst);
  24.                         }
  25.                 }
  26.                 return sum;
  27.         }
  28.        
  29.         private static int cal(int sum, int num, char op, boolean isFirst) {
  30.                 if (isFirst)
  31.                         return num;
  32.                 else
  33.                         return (op == '+' ? sum + num : sum * num);
  34.         }
复制代码

评分

参与人数 1大米 +3 收起 理由
hulahu + 3 感谢分享!

查看全部评分

回复

使用道具 举报

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

本版积分规则

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