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

Bloomberg 电面

🔗
muyue250 2019-3-14 06:38:39 | 只看该作者
全局:
自己写的没用Map也没用Stack的方法,不知有没有问题。假设输入的string是有效的(不会出现括号乱序)、结果不需要去重
  1. public List<String> deepestStr(String input) {
  2.   // Corner case
  3.   List<String> deepest = new ArrayList<>(); // Use set if dedup is required
  4.   if (input == null || input.length() == 0) {
  5.     return deepest;
  6.   }

  7.   int level = 0;
  8.   int global_max = 0;
  9.   // If we need to validate the combination of parenthesis, use a stack
  10.   char[] array = input.toCharArray();
  11.   StringBuilder sb = new StringBuilder();
  12.   for (int i = 0; i < array.length; i++) {
  13.     if (array[i] == '('|| array[i] == '{' || array[i] == '[') {
  14.       // Ignore word before, level goes deeper
  15.       level ++;
  16.       sb = new StringBuilder();      
  17.     } else if (array[i] == ')'|| array[i] == '}' || array[i] == ']') {
  18.       if (level > global_max) {
  19.         global_max = level;
  20.         deepest.clear();
  21.         deepest.add(sb.toString());
  22.       } else if (level == global_max) {
  23.         deepest.add(sb.toString());
  24.       }
  25.       level --;
  26.       sb = new StringBuilder();
  27.     } else {
  28.       sb.append(array[i]);
  29.     }
  30.   }
  31.   // Corner case, if no parenthesis
  32.   if (deepest.isEmpty()) {
  33.     deepest.add(sb.toString());
  34.   }
  35.   return deepest;
  36. }
复制代码



回复

使用道具 举报

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

本版积分规则

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