DFS & BFS:
关键在于构造graph
package twoSigma; import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue; public class FriendCircle1 {
ArrayList<ArrayList<Integer>> graph;
public int friendCircles(String[] friends) {
graph = new ArrayList<ArrayList<Integer>>();
for (int i=0; i<friends.length; i++) {
ArrayList<Integer> item = new ArrayList<Integer>();
for (int j=0; j<friends[0].length(); j++) {
if (i!=j && friends[i].charAt(j) == 'Y') {
item.add(j);
}
}
graph.add(new ArrayList<Integer>(item));
}
HashSet<Integer> visited = new HashSet<Integer>();
int res = 0;
for (int i=0; i<graph.size(); i++) {
if (!visited.contains(i)) {
res++;
//bfs(i, visited);
dfs(i, visited);
}
}
return res;
} public void bfs(int i, HashSet<Integer> visited) {
visited.add(i);
Queue<Integer> q = new LinkedList<Integer>();
q.offer(i);
while (!q.isEmpty()) {
int cur = q.poll();
ArrayList<Integer> curFriends = graph.get(cur);
for (int each : curFriends) {
if (!visited.contains(each)) {
visited.add(each);
q.offer(each);
}
}
}
} public void dfs(int i, HashSet<Integer> visited) {
if (visited.contains(i)) return;
visited.add(i);
ArrayList<Integer> friends = graph.get(i);
for (int friend : friends) {
dfs(friend, visited);
}
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FriendCircle1 sol = new FriendCircle1();
//String[] input = new String[]{"YNYNNN", "NYNYNN", "YNYNNN", "NYNYNN", "NNNNYY", "NNNNYY"};
//String[] input = new String[]{"YYNN", "YYYN", "NYYN", "NNNY"};
//String[] input = new String[]{"YNN", "NYN", "NNY"};
String[] input = new String[]{"YYY", "YYY", "YYY"};
int count = sol.friendCircles(input);
System.out.println(count);
} }
如果想把String[] array转化为2d char array, code如下,写的时候这里出了点小问题。friends是String[]
char[][] arr = new char[friends.length][];
for (int i=0; i<friends.length; i++) {
arr[i] = friends[i].toCharArray();
}
Union Find
package twoSigma; public class FriendCircle2 {
public int friendCircles(String[] friends) {
Unionfind uf = new Unionfind(friends.length);
for (int i=0; i<friends.length; i++) {
for (int j=0; j<friends[i].length(); j++) {
if (i != j && friends[i].charAt(j)=='Y')
uf.union(i, j);
}
}
return uf.count;
} public class Unionfind {
int[] ids;
int count;
public Unionfind(int num) {
this.ids = new int[num];
this.count = num;
for (int i=0; i<num; i++) {
ids[i] = i;
}
} public int findId(int i) {
return ids[i];
} public boolean isConnected(int i1, int i2) {
return findId(i1)==findId(i2);
} public boolean union(int i1, int i2) {
if (!isConnected(i1, i2)) {
for (int i=0; i<ids.length; i++) {
if (ids[i] == findId(i2)) {
ids[i] = findId(i1);
}
}
count--;
return true;
}
return false;
}
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FriendCircle2 sol = new FriendCircle2();
String[] input = new String[]{"YNYNNN", "NYNYNN", "YNYNNN", "NYNYNN", "NNNNYY", "NNNNYY"};
//String[] input = new String[]{"YYNN", "YYYN", "NYYN", "NNNY"};
//String[] input = new String[]{"YNN", "NYN", "NNY"};
//String[] input = new String[]{"YYY", "YYY", "YYY"};
int count = sol.friendCircles(input);
System.out.println(count);
} }