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

Twitter电面

全局:

2016(1-3月) 码农类General 硕士 全职@twitter - 内推 - 技术电面  | | Other | 应届毕业生

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
发一波面筋攒RP!考的是在一个有括号的string里找longest well formed subsequence, 比如“((()(
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
5):
啊啊啊~发完面筋就来二面了~烙印好nice啊~感动

评分

参与人数 2大米 +16 收起 理由
a598165394 + 1 感谢分享!
爱丽丝和鲍勃 + 15

查看全部评分


上一篇:Amazon电面被放鸽子?
下一篇:3/22 Google 第一轮电面

本帖被以下淘专辑推荐:

全局:
norafang 发表于 2016-3-24 09:14
比如说(())和()()这两种情况的话,后一种用这个方法输出的就是()(),那前一种第二次pop“(”的时候你怎 ...

def longestSubsequence(s):
     #use stack and a int to record maxlength
     #if the stack append again, we need to recalculate the maxlength
    maxlength = 0
    length = 0
    stack = []
    rs = ""
    maxrs = ""
    for c in s:
        if not stack:
            if c ==")":
                length = 0
                rs = ""
            else:
                stack.append(c)
                rs +="("
        else:
            if c == "(":
                stack.append(c)
                rs +="("
            else:
                if stack.pop() == "(":
                    length +=2
                    rs +=")"
                # else:
                #     length = 0
        if len(maxrs)<len(rs):
            maxrs = rs
        if maxlength<length:
            maxlength = length
    return maxlength,maxrs

print longestSubsequence("())()()")

sorry我题目看错了。。其实道理一样。你可以用一个string来记录当前走过的parenthesis。然后判断stirng的长度,保留长度最长那个。这样其实就不用maxlength了。。我一直以为是要求最大长度。
回复

使用道具 举报

推荐
sealove999 2016-3-27 02:03:06 | 只看该作者
全局:
sealove999 发表于 2016-3-27 01:55
楼主是这个代码的意思么

刚才那个是错的,看这个
  1. public class Solution {
  2.   public String findLongestWellFormatedSubstring(String s) {
  3.     String ret = "";
  4.     Deque<Integer> stack = new ArrayDeque<>();
  5.     for (int i = 0; i < s.length(); i++) {
  6.       if (s.charAt(i) == ')' && (!stack.isEmpty() && s.charAt(stack.peek()) == '(')) {
  7.         stack.pop();
  8.         int idx = stack.isEmpty() ? -1 : stack.peek();
  9.         if (i - idx > ret.length()) {
  10.           ret = s.substring(idx + 1, i + 1);
  11.         }
  12.       } else {
  13.         stack.push(i);
  14.       }
  15.     }
  16.     return ret;
  17.   }

  18.   public int findLongestWellFormatedSubstringLength(String s) {
  19.     int ret = 0;
  20.     Deque<Integer> stack = new ArrayDeque<>();
  21.     for (int i = 0; i < s.length(); i++) {
  22.       if (s.charAt(i) == '(') {
  23.         stack.push(i);
  24.       } else if (s.charAt(i) == ')') {
  25.         if (!stack.isEmpty() && s.charAt(stack.peek()) == '(') {
  26.           stack.pop();
  27.           ret = Math.max(ret, i - (stack.isEmpty() ? -1 : stack.peek()));
  28.         } else {
  29.           stack.push(i);
  30.         }
  31.       }
  32.     }
  33.     return ret;
  34.   }

  35.   public static void main(String[] args) {
  36.     Solution ss = new Solution();
  37.     String s = ")(()())";
  38.     System.out.println(ss.findLongestWellFormatedSubstringLength(s));
  39.     System.out.println(ss.findLongestWellFormatedSubstring(s));
  40.   }
  41. }
复制代码
回复

使用道具 举报

推荐
sealove999 2016-3-27 01:55:42 | 只看该作者
全局:
楼主是这个代码的意思么


  1. public class Solution {
  2.   public String findLongestWellFormatedSubstring(String s) {
  3.     String ret = "";
  4.     Deque<Integer> stack = new ArrayDeque<>();
  5.     for (int i = 0; i < s.length(); i++) {
  6.       if (s.charAt(i) == '(') {
  7.         stack.push(i);
  8.       } else if (s.charAt(i) == ')') {
  9.         if (!stack.isEmpty() && s.charAt(stack.peek()) == '(') {
  10.           stack.pop();
  11.           int idx = stack.isEmpty() ? -1 : stack.peek();
  12.           if (i - idx > ret.length()) {
  13.             ret = s.substring(idx + 1, i + 1);
  14.           }
  15.         }
  16.       }
  17.     }
  18.     return ret;
  19.   }

  20.   public int findLongestWellFormatedSubstringLength(String s) {
  21.     int ret = 0;
  22.     Deque<Integer> stack = new ArrayDeque<>();
  23.     for (int i = 0; i < s.length(); i++) {
  24.       if (s.charAt(i) == '(') {
  25.         stack.push(i);
  26.       } else if (s.charAt(i) == ')') {
  27.         if (!stack.isEmpty() && s.charAt(stack.peek()) == '(') {
  28.           stack.pop();
  29.           ret = Math.max(ret, i - (stack.isEmpty() ? -1 : stack.peek()));
  30.         }
  31.       }
  32.     }
  33.     return ret;
  34.   }

  35.   public static void main(String[] args) {
  36.     Solution ss = new Solution();
  37.     String s = "((()())";
  38.     System.out.println(ss.findLongestWellFormatedSubstringLength(s));
  39.     System.out.println(ss.findLongestWellFormatedSubstring(s));
  40.   }
  41. }
复制代码
回复

使用道具 举报

🔗
gimook 2016-3-23 05:18:28 | 只看该作者
全局:
lz电面之前收到5分钟的pre-interview question了吗?
回复

使用道具 举报

🔗
 楼主| norafang 2016-3-23 05:31:41 | 只看该作者
全局:
gimook 发表于 2016-3-23 05:18
lz电面之前收到5分钟的pre-interview question了吗?

收到啦~~~~~~~~~~~~
回复

使用道具 举报

🔗
attt 2016-3-23 05:39:14 | 只看该作者
全局:
lz没有oa 直接电面?
回复

使用道具 举报

🔗
ahwhdsl 2016-3-23 05:48:06 | 只看该作者
全局:
gimook 发表于 2016-3-23 05:18
lz电面之前收到5分钟的pre-interview question了吗?

前天晚上做的oa,刚才收到pre-interview question了
回复

使用道具 举报

🔗
jiebour 2016-3-23 05:51:50 | 只看该作者
全局:
ahwhdsl 发表于 2016-3-23 05:48
前天晚上做的oa,刚才收到pre-interview question了

楼主怎么拿到机会的?内推?thanks!
回复

使用道具 举报

🔗
youhusky 2016-3-23 05:58:36 | 只看该作者
全局:
同收到pre-interview question
回复

使用道具 举报

🔗
小飞羊 2016-3-23 06:00:30 | 只看该作者
全局:
求Are there any projects that you're interested in working on at Twitter?这一栏选projects的建议
回复

使用道具 举报

🔗
小飞羊 2016-3-23 06:01:20 | 只看该作者
全局:
lz是怎么选Are there any projects that you're interested in working on at Twitter?这里面的project的啊
回复

使用道具 举报

🔗
a598165394 2016-3-23 06:16:34 | 只看该作者
全局:
楼主,能否问一下这道题是要return "(()())" 还是要return 6啊?

补充内容 (2016-3-23 06:20):
Never mind,i see.本质都是一样的嗯。。谢谢了啊
回复

使用道具 举报

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

本版积分规则

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