本文整理匯總了Java中edu.princeton.cs.algs4.SET類的典型用法代碼示例。如果您正苦於以下問題:Java SET類的具體用法?Java SET怎麽用?Java SET使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
SET類屬於edu.princeton.cs.algs4包,在下文中一共展示了SET類的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: simple
import edu.princeton.cs.algs4.SET; //導入依賴的package包/類
/**
* Returns a random simple graph containing <tt>V</tt> vertices and <tt>E</tt> edges.
* @param V the number of vertices
* @param E the number of vertices
* @return a random simple graph on <tt>V</tt> vertices, containing a total
* of <tt>E</tt> edges
* @throws IllegalArgumentException if no such simple graph exists
*/
public static Graph simple(int V, int E) {
if (E > (long) V*(V-1)/2) throw new IllegalArgumentException("Too many edges");
if (E < 0) throw new IllegalArgumentException("Too few edges");
Graph G = new Graph(V);
SET<Edge> set = new SET<Edge>();
while (G.E() < E) {
int v = StdRandom.uniform(V);
int w = StdRandom.uniform(V);
Edge e = new Edge(v, w);
if ((v != w) && !set.contains(e)) {
set.add(e);
G.addEdge(v, w);
}
}
return G;
}
示例2: getAllValidWords
import edu.princeton.cs.algs4.SET; //導入依賴的package包/類
public Iterable<String> getAllValidWords(BoggleBoard board) {
this.board = board;
rows = board.rows();
cols = board.cols();
adj = (Bag<Integer>[]) new Bag[rows*cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int v = i*cols + j;
adj[v] = new Bag<Integer>();
if (checkWIsValid(i-1, j)) adj[v].add((i-1) * cols + j);
if (checkWIsValid(i+1, j)) adj[v].add((i+1) * cols + j);
if (checkWIsValid(i, j+1)) adj[v].add(i * cols + j+1);
if (checkWIsValid(i, j-1)) adj[v].add(i * cols + j-1);
if (checkWIsValid(i+1, j-1)) adj[v].add((i+1) * cols + j-1);
if (checkWIsValid(i+1, j+1)) adj[v].add((i+1) * cols + j+1);
if (checkWIsValid(i-1, j-1)) adj[v].add((i-1) * cols + j-1);
if (checkWIsValid(i-1, j+1)) adj[v].add((i-1) * cols + j+1);
}
}
validWords = new SET<String>();
for (int v = 0; v < rows*cols; v++) {
visitingDices = new Stack<Integer>();
marked = new boolean[rows*cols];
visitingDices.push(v);
marked[v] = true;
if(getLetterOnBoard(v) == 'Q')
searchValidWords(v, root.next['Q'-'A'].next['U' - 'A'], "QU", visitingDices);
else
searchValidWords(v, root.next[getLetterOnBoard(v)-'A'], getLetterOnBoard(v) + "", visitingDices);
}
return validWords;
}