有一些账号,账号里面有一个或多个email, 如果两个账号有共同的email,则认为这两个账号是同一个人,找出哪些账号是同一个人
输入是这样的:数字是用户,字母是邮箱,有很多人有多个邮箱,找出相同的用户
1- {x,y,z}
2-{x}
3-{a,b}
4-{y, z}
5-{b}
6-{m}
7-{t,b}

账号之间如果有公共email就连边,我觉得要用hashMap, 存(email, user) pair

如果当前user的某个email在hashMap里出现过,当时是user1, 就看user和user1是否connected,否的话就要union,根据Union Find

Union Find Based on Quick Union

 package fbOnsite;
import java.util.*;
class UnionFind{
int[] ids;
int count;
public UnionFind(int num) {
ids = new int[num];
Arrays.fill(ids, -1);
count = 0;
} public int find(int id) {
while (id != ids[id]) id = ids[id];
return id;
} public void union(int n1, int n2) {
int root1 = find(n1);
int root2 = find(n2);
ids[root2] = root1;
} public boolean isConnected(int n1, int n2) {
return find(n1) == find(n2);
}
} public class EmailUser {
public int commonUser(HashMap<Integer, List<Character>> map) {
int size = map.size();
UnionFind uf = new UnionFind(10);
HashMap<Character, Integer> emailToUser = new HashMap<Character, Integer>();
for (int user : map.keySet()) {
uf.ids[user] = user;
uf.count++;
List<Character> emailList = map.get(user);
for (Character eachEmail : emailList) {
if (!emailToUser.containsKey(eachEmail)) {
emailToUser.put(eachEmail, user);
}
else {
int oriUser = emailToUser.get(eachEmail);
if (!uf.isConnected(user, oriUser)) {
uf.union(oriUser, user);
uf.count--;
}
}
}
}
for (int elem : uf.ids)
System.out.print(elem);
return uf.count;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
EmailUser sol = new EmailUser();
HashMap<Integer, List<Character>> map = new HashMap<Integer, List<Character>>();
map.put(0, new ArrayList<Character>());
map.put(1, new ArrayList<Character>());
map.put(2, new ArrayList<Character>());
map.put(3, new ArrayList<Character>());
map.put(4, new ArrayList<Character>());
map.put(5, new ArrayList<Character>());
map.put(6, new ArrayList<Character>());
map.get(0).add('x');
map.get(0).add('y');
map.get(0).add('z');
map.get(1).add('x');
map.get(2).add('a');
map.get(2).add('b');
map.get(3).add('y');
map.get(3).add('z');
map.get(4).add('b');
map.get(5).add('m');
map.get(6).add('t');
map.get(6).add('b');
int res = sol.commonUser(map);
System.out.println(res);
} }

FB面经Prepare: Email User的更多相关文章

  1. FB面经 Prepare: All Palindromic Substrings

    Given a string, calculate how many substring is palindrome. Ignore non-char characters. Ignore case; ...

  2. FB面经 Prepare: Task Schedule

    tasks has cooldown time, give an input task id array, output finish time input: AABCA A--ABCA output ...

  3. FB面经 Prepare: Make Parentheses valid

    给一组括号,remove最少的括号使得它valid 从左从右各scan一次 package fb; public class removeParen { public static String fi ...

  4. FB面经Prepare: Friends Recommendation

    有个getFriend() API, 让你推荐你的朋友的朋友做你的朋友,当然这个新朋友不能是你原来的老朋友 package fb; import java.util.*; public class R ...

  5. FB面经Prepare: Dot Product

    Conduct Dot Product of two large Vectors 1. two pointers 2. hashmap 3. 如果没有额外空间,如果一个很大,一个很小,适合scan小的 ...

  6. FB面经prepare: Count the number of Vector

    给一个超级大的排好序的vector [abbcccdddeeee]比如,要求返回[{,a}, {,b}, {,c}, {,d}, {,e}......]复杂度要优于O(N) 分析: 如果是binary ...

  7. FB面经 Prepare: Largest Island

    Find largest island in a board package fb; public class LargestIsland { public int findLargestIsland ...

  8. FB面经prepare: task schedule II

    followup是tasks是无序的. 一开始是有序的,比如说1, 1, 2, 1,一定要先执行第一个task1,然后等task1恢复,再执行第2个task1,再执行task2..... follow ...

  9. FB面经prepare: Task Schedule

    每种task都有冷却时间,比如task1执行后,要经过interval时间后才能再次执行,求总共所需时间. 用HashMap保存每一个task的下一次可以开始执行的最早时间 package TaskS ...

随机推荐

  1. python 10大算法之一 LinearRegression 笔记

    简单的线性回归预测房价 #!/usr/bin/env python # encoding: utf-8 """ @version: @author: --*--. @fi ...

  2. 使用mybatis assembly插件打成tar包,在linux系统中运行服务

    使用mybatis assembly插件打成tar包,在linux系统中运行服务 assembly插件插件地址: 链接:https://pan.baidu.com/s/1i6bWPxF 密码:gad5 ...

  3. Phone List 字典树 OR STL

    Phone List Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 140     Solved: 35 Description ...

  4. JS功能函数

    1.整数每三位增加一个, function toThousands(num) {      return (num || 0).toString().replace(/(\d)(?=(?:\d{3}) ...

  5. angular.isString()

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. 在页面上获取web项目信息

    获取协议名称:request.getScheme() 获取域名:request.getServerName() 获取项目名称:request.getContextPath() 使用EL表达式获取项目名 ...

  7. PermissionDispatcher 运行时权限框架

    第一步在app的build.gradle文件中添加: dependencies { // PermissionDispatcher 框架的使用 implementation 'com.github.h ...

  8. 增值税发票 成都金锐发票IC卡读入

    黑盘-操作  打开发票领购---网络发票分发----点击查询--分发. 白盘操作:

  9. CDN请求失败,请求本地

    方法一: <script src="http://lib.sinaapp.com/js/jquery11/1.8/jquery.min.js"></script& ...

  10. python 读取文本文档中的数据

    import os dir = input('Please input the file dir:')#提示输入文件路径 while not os.path.exists(dir):#判断文件是否存在 ...