标签:style blog color java io 数据 for ar
1st round: 一个白人,先是聊了下project,然后一道coding题,找到距离一个节点k的所有节点。这个直接node bfs就可以解决。这一轮很基础。
node bfs 加 count 计算level就行.
facebook电面原题
2nd round: 一个印度小伙,上来就是题,没有behavior question。
第一题设计手机通讯录用什么数据结构,这题用Trie解决,然后让我实现了Trie的search method。
import java.util.ArrayList; import java.util.Arrays; class Node{ String number; boolean end; Node[] next = new Node[26]; Node(){ for(int i=0; i<26;i++) next[i] = null; } public void insert(String s,String number){ Node cur = this; for(int i=0; i<s.length();i++){ if(cur.next[s.charAt(i)-‘a‘] == null) { cur.next[s.charAt(i)-‘a‘] = new Node(); } cur = cur.next[s.charAt(i)-‘a‘]; } cur.end = true; cur.number = number; } public String search(String s){ Node cur = this; for(int i=0; i<s.length();i++){ if(cur.next[s.charAt(i)-‘a‘] == null) { return null; } cur = cur.next[s.charAt(i)-‘a‘]; } if(cur.end != true) return null; else return cur.number; } } public class Solution { public static void main(String[] args) { Node head = new Node(); head.insert("abc", "323-1"); head.insert("bcde", "323-2"); head.insert("abcfs", "323-3"); head.insert("z", "323-4"); head.insert("abcfggg", "323-5"); System.out.print(head.search("bcde")); } }
第二题是关于集合intersection的,有两个集合,要求找出在第一个集合出现p次,在第二个集合出现q次的所有元素。这个用hashtable就能解决。
import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Map.Entry; import javax.swing.text.html.HTMLDocument.Iterator; class Intersection{ public ArrayList<Integer> findIntersection(int[] ary1, int[] ary2, int p1, int p2){ HashMap<Integer,Integer> map = new HashMap<Integer,Integer>(); ArrayList<Integer> ret = new ArrayList<Integer>(); for(int i=0; i<ary1.length;i++){ if(!map.containsKey(ary1[i])){ map.put(ary1[i], 1); }else{ int count = map.get(ary1[i]); count++; map.put(ary1[i], count); } } //delete all from array1 where values not equal p1 //java.util.ConcurrentModificationException if direct delete from map. HashMap<Integer,Integer> map2 = new HashMap<Integer,Integer>(); for(Entry<Integer,Integer> entry : map.entrySet()){ if(entry.getValue()==p1) map2.put(entry.getKey(), 0); } for(int i=0; i<ary2.length;i++){ if(!map2.containsKey(ary2[i])){ continue; }else{ int count = map2.get(ary2[i]); count++; map2.put(ary2[i], count); } } for(Entry<Integer,Integer> entry : map2.entrySet()){ if(entry.getValue()==p2) ret.add(entry.getKey()); } return ret; } } public class Solution { public static void main(String[] args) { Intersection inter = new Intersection(); int[] ary1 = {1,2,2,2,3,3,3,4,5,6}; int[] ary2 = {111,1,1,2,2,3,3,44,4,4,5,5,6}; ArrayList<Integer> r = inter.findIntersection(ary1, ary2, 3, 2); for(int i=0;i<r.size();i++){ System.out.print(" " + r.get(i)); } } }
标签:style blog color java io 数据 for ar
原文地址:http://www.cnblogs.com/leetcode/p/3896166.html