首页 > 代码库 > 2Sigma OA prepare: Friends Circle
2Sigma OA prepare: Friends Circle
DFS & BFS:
关键在于构造graph
1 package twoSigma; 2 3 import java.util.ArrayList; 4 import java.util.HashSet; 5 import java.util.LinkedList; 6 import java.util.Queue; 7 8 public class FriendCircle1 { 9 ArrayList<ArrayList<Integer>> graph; 10 public int friendCircles(String[] friends) { 11 graph = new ArrayList<ArrayList<Integer>>(); 12 for (int i=0; i<friends.length; i++) { 13 ArrayList<Integer> item = new ArrayList<Integer>(); 14 for (int j=0; j<friends[0].length(); j++) { 15 if (i!=j && friends[i].charAt(j) == ‘Y‘) { 16 item.add(j); 17 } 18 } 19 graph.add(new ArrayList<Integer>(item)); 20 } 21 HashSet<Integer> visited = new HashSet<Integer>(); 22 int res = 0; 23 for (int i=0; i<graph.size(); i++) { 24 if (!visited.contains(i)) { 25 res++; 26 //bfs(i, visited); 27 dfs(i, visited); 28 } 29 } 30 return res; 31 } 32 33 public void bfs(int i, HashSet<Integer> visited) { 34 visited.add(i); 35 Queue<Integer> q = new LinkedList<Integer>(); 36 q.offer(i); 37 while (!q.isEmpty()) { 38 int cur = q.poll(); 39 ArrayList<Integer> curFriends = graph.get(cur); 40 for (int each : curFriends) { 41 if (!visited.contains(each)) { 42 visited.add(each); 43 q.offer(each); 44 } 45 } 46 } 47 } 48 49 public void dfs(int i, HashSet<Integer> visited) { 50 if (visited.contains(i)) return; 51 visited.add(i); 52 ArrayList<Integer> friends = graph.get(i); 53 for (int friend : friends) { 54 dfs(friend, visited); 55 } 56 } 57 58 /** 59 * @param args 60 */ 61 public static void main(String[] args) { 62 // TODO Auto-generated method stub 63 FriendCircle1 sol = new FriendCircle1(); 64 //String[] input = new String[]{"YNYNNN", "NYNYNN", "YNYNNN", "NYNYNN", "NNNNYY", "NNNNYY"}; 65 //String[] input = new String[]{"YYNN", "YYYN", "NYYN", "NNNY"}; 66 //String[] input = new String[]{"YNN", "NYN", "NNY"}; 67 String[] input = new String[]{"YYY", "YYY", "YYY"}; 68 int count = sol.friendCircles(input); 69 System.out.println(count); 70 } 71 72 }
如果想把String[] array转化为2d char array, code如下,写的时候这里出了点小问题。friends是String[]
1 char[][] arr = new char[friends.length][]; 2 for (int i=0; i<friends.length; i++) { 3 arr[i] = friends[i].toCharArray(); 4 }
Union Find
1 package twoSigma; 2 3 public class FriendCircle2 { 4 public int friendCircles(String[] friends) { 5 Unionfind uf = new Unionfind(friends.length); 6 for (int i=0; i<friends.length; i++) { 7 for (int j=0; j<friends[i].length(); j++) { 8 if (i != j && friends[i].charAt(j)==‘Y‘) 9 uf.union(i, j); 10 } 11 } 12 return uf.count; 13 } 14 15 public class Unionfind { 16 int[] ids; 17 int count; 18 public Unionfind(int num) { 19 this.ids = new int[num]; 20 this.count = num; 21 for (int i=0; i<num; i++) { 22 ids[i] = i; 23 } 24 } 25 26 public int findId(int i) { 27 return ids[i]; 28 } 29 30 public boolean isConnected(int i1, int i2) { 31 return findId(i1)==findId(i2); 32 } 33 34 public boolean union(int i1, int i2) { 35 if (!isConnected(i1, i2)) { 36 for (int i=0; i<ids.length; i++) { 37 if (ids[i] == findId(i2)) { 38 ids[i] = findId(i1); 39 } 40 } 41 count--; 42 return true; 43 } 44 return false; 45 } 46 } 47 48 /** 49 * @param args 50 */ 51 public static void main(String[] args) { 52 // TODO Auto-generated method stub 53 FriendCircle2 sol = new FriendCircle2(); 54 String[] input = new String[]{"YNYNNN", "NYNYNN", "YNYNNN", "NYNYNN", "NNNNYY", "NNNNYY"}; 55 //String[] input = new String[]{"YYNN", "YYYN", "NYYN", "NNNY"}; 56 //String[] input = new String[]{"YNN", "NYN", "NNY"}; 57 //String[] input = new String[]{"YYY", "YYY", "YYY"}; 58 int count = sol.friendCircles(input); 59 System.out.println(count); 60 } 61 62 }
2Sigma OA prepare: Friends Circle
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。