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

Uber一面感觉要跪

🔗
xmasry 2017-12-7 15:26:46 | 只看该作者
全局:
  1. public String format(String name, String[] array){
  2.                 if(array.length == 0 || name == null || name.length() == 0){
  3.                         return name;
  4.                 }
  5.                 Map<Character, Set<String>> dict = getDict(array);
  6.                 StringBuilder sb = new StringBuilder();
  7.                 for(int i=0; i<name.length(); ){
  8.                         Character c = new Character(name.charAt(i));
  9.                         String str = match(c, i, name, dict);
  10.                         if(str.length()==0){
  11.                                 sb.append(c);
  12.                                 i++ ;
  13.                         }else{
  14.                                 sb.append("[").append(str).append("]");
  15.                                 i+=str.length();
  16.                         }
  17.                 }
  18.                 return sb.toString();
  19.         }
  20.        
  21.         private String match(Character c, int index, String name, Map<Character, Set<String>> dict){
  22.                 String maxMatch = "";
  23.                 if(!dict.containsKey(c)){
  24.                         return maxMatch;
  25.                 }
  26.                 Set<String> set = dict.get(c);
  27.                
  28.                 for(String s: set){
  29.                         if(s.length() <= (name.length() - index)
  30.                                         && name.indexOf(s) == index && s.length()>maxMatch.length()){                       
  31.                                         maxMatch = s;
  32.                         }
  33.                 }
  34.                
  35.                 if(maxMatch.length()!=0){
  36.                         maxMatch = maxMatch.replaceFirst(c.toString(), c.toString().toUpperCase());                       
  37.                 }
  38.                
  39.                 return maxMatch;
  40.         }
  41.        
  42.         private Map<Character, Set<String>> getDict(String[] array){
  43.                 Map<Character, Set<String>> dict =  new HashMap<>();
  44.                 for(String str:array){
  45.                         str = str.toLowerCase();
  46.                         Character c = new Character(str.charAt(0));
  47.                         Set<String> set=dict.get(c);
  48.                         if(set == null){
  49.                                 set = new HashSet<String>();
  50.                                 dict.put(c, set);
  51.                         }
  52.                         set.add(str.toLowerCase());
  53.                 }
  54.                 return dict;
  55.         }
复制代码
回复

使用道具 举报

🔗
leetcodeguy 2017-12-29 11:52:21 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
ChrisGates23 2018-1-2 00:57:04 | 只看该作者
本楼:
全局:
mark一下
回复

使用道具 举报

🔗
ChrisGates23 2018-1-2 04:06:51 | 只看该作者
全局:
abcdef: ['a', 'c', 'e', 'bcd'] 这个是 [a]b[c]d[e]f 还是 a[bcd]ef
回复

使用道具 举报

🔗
qqfight2017 2018-1-9 15:15:06 | 只看该作者
全局:
Thanks for sharing!
回复

使用道具 举报

🔗
creekwhisper 2018-1-9 15:34:59 | 只看该作者
全局:
uber这么难啊。。。。
回复

使用道具 举报

🔗
zhxymacau2017 2018-1-15 17:29:42 | 只看该作者
全局:
  1. class Main {
  2.   static class TrieNode {
  3.       char key;
  4.       TrieNode[] children;
  5.       boolean hasword;
  6.       TrieNode(Character key) {
  7.          
  8.           children = new TrieNode[26];
  9.           this.key = key;
  10.           hasword = false;
  11.       }
  12.       TrieNode() {
  13.           this.key = ' ';
  14.           children = new TrieNode[26];
  15.           hasword = false;
  16.       }
  17.   }
  18.   
  19.   static class TrieTree {
  20.       TrieNode root;
  21.       TrieTree() {
  22.           root = new TrieNode();
  23.       }
  24.       public void insert(String word) {
  25.           TrieNode cur = root;
  26.           for (int i = 0; i < word.length(); i++) {
  27.               if (cur.children[word.charAt(i) - 'a'] == null)
  28.                   cur.children[word.charAt(i) - 'a'] = new TrieNode(word.charAt(i));
  29.               cur = cur.children[word.charAt(i) - 'a'];
  30.           }
  31.           cur.hasword = true;
  32.       }
  33.   }
  34.   private static String searchWords(String[] dict, String str) {
  35.       TrieTree trietree = new TrieTree();
  36.       for (int i = 0; i < dict.length; i++) {
  37.           trietree.insert(dict[i]);
  38.       }
  39.       StringBuilder sb = new StringBuilder();
  40.       int slow = 0, fast = 0;
  41.       while (fast < str.length()) {
  42.           TrieNode cur = trietree.root;
  43.           int counter = 0;
  44.           while (fast < str.length() && cur.children[str.charAt(fast) - 'a'] != null) {
  45.               cur = cur.children[str.charAt(fast) - 'a'];
  46.               counter++;
  47.               fast++;
  48.           }
  49.           if (counter > 0) {//wrong: 忘记回退了.
  50.               fast--;
  51.               counter = 0;
  52.           }
  53.           if (cur.hasword) {//hasword 用于区分ddt是个词,但是不是个完整的词这个情况
  54.               int i = slow;
  55.               sb.append("[").append(Character.toUpperCase(str.charAt(i++)));
  56.               while (i < fast) {
  57.                   sb.append(str.charAt(i++));
  58.               }
  59.               sb.append("]");
  60.           } else {
  61.               sb.append(str.substring(slow, fast + 1));
  62.           }
  63.           fast++;
  64.           slow = fast;
  65.          
  66.       }
  67.       return sb.toString();
  68.   }
  69.   
  70.   public static void main(String[] args) {
  71.     String[] dict = {"b", "bc","cde", "e","tt","ertt","ddr"};
  72.     String str = "abcdeddertte";
  73.     System.out.println(searchWords(dict, str));
  74.   }
  75. }
复制代码
回复

使用道具 举报

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

本版积分规则

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