Cousera
绿色小光头
这个作者很懒,什么都没留下…
展开
-
Percolation
PercolationPercolation.java主要问题为backwash。其形成原因为:因为存在上下两个虚拟节点,当percolates()为true上下贯通时,所有在最底排的已经打开的节点都会变成与上虚拟节点连通(每打开一个底部节点,都会将其与下虚拟节点连通)。解决方法是再设置一个union-find,但只提供上虚拟节点,将isFull()放在第二个union-find中进行判断。...原创 2019-06-15 10:47:18 · 566 阅读 · 0 评论 -
WordNet
WordNetWordNet.java问题主要是判断G是不是只有一个根的有向无环图。无环可以通过BFS或DFS去判断,这里使用内置的类DirectedCycle去判断;不难看出一个点是根的条件是它的出度为0,而在无环的前提下,每一个连通分量必然至少有一个出度为0的点(反证法很容易证明),所以只要判断出只有一个出度为0的点,则既能确定该有向图为连通图,又能确定该图只有一个根。import ed...原创 2019-09-05 14:04:30 · 202 阅读 · 0 评论 -
Interview Questions: Directed Graphs
Directed GraphsShortest directed cycle. Given a digraph G, design an efficient algorithm to find a directed cycle with the minimum number of edges (or report that the graph is acyclic). The running ...原创 2019-09-04 21:22:58 · 125 阅读 · 0 评论 -
Interview Questions: Undirected Graphs
Undirected GraphsNonrecursive depth-first search. Implement depth-first search in an undirected graph without using recursion.非递归实现DFS。public void DFS(Graph G, int v, boolean[] marked) { ...原创 2019-08-30 21:24:31 · 190 阅读 · 0 评论 -
Interview Questions: Hash Tables
Hash Tables4-SUM. Given an array a[] of nn integers, the 4-SUM problem is to determine if there exist distinct indices i, j, k, and l such that a[i]+a[j]=a[k]+a[l]. Design an algorithm for the 4-SUM...原创 2019-08-22 22:08:03 · 224 阅读 · 0 评论 -
Kd-Trees
Kd-TreesPointSET.java暴力法没啥好说的,红黑树都用的现成的,照着API写就完事儿了,注意异常的抛出。import edu.princeton.cs.algs4.Point2D;import edu.princeton.cs.algs4.RectHV;import java.util.ArrayList;import java.util.List;import j...原创 2019-08-22 12:00:39 · 149 阅读 · 0 评论 -
Interview Questions: Balanced Search Trees
Balanced Search TreesRed–black BST with no extra memory. Describe how to save the memory for storing the color information when implementing a red–black BST.Hint: modify the structure of the BST to...原创 2019-08-16 20:03:47 · 322 阅读 · 0 评论 -
Interview Questions: Elementary Symbol Tables
Elementary Symbol TablesJava autoboxing and equals(). Consider two原创 2019-08-02 14:42:53 · 218 阅读 · 0 评论 -
8 Puzzle
8 PuzzleBoard.java照着API写即可。注意每次调用twin()都要返回相同的值,即twin()只能交换两个固定位置的数字。import edu.princeton.cs.algs4.StdRandom;import java.util.ArrayList;import java.util.List;public class Board { private f...原创 2019-07-31 11:49:27 · 793 阅读 · 0 评论 -
Interview Questions: Quicksort
QuicksortNuts and bolts. A disorganized carpenter has a mixed pile of n nuts and n bolts. The goal is to find the corresponding pairs of nuts and bolts. Each nut fits exactly one bolt and each bolt ...原创 2019-07-21 22:59:08 · 197 阅读 · 0 评论 -
Interview Questions: Priority Queues
Priority QueuesDynamic median. Design a data type that supports insert in logarithmic time, find-the-median in constant time, and remove-the-median in logarithmic time. If the number of keys in the ...原创 2019-07-29 22:44:03 · 213 阅读 · 0 评论 -
Collinear Points
Collinear PointsPoint.java照要求补API即可。import java.util.Comparator;import edu.princeton.cs.algs4.StdDraw;public class Point implements Comparable<Point> { private final int x; // x-c...原创 2019-07-19 16:33:54 · 631 阅读 · 0 评论 -
Interview Questions: Mergesort
MergesortMerging with smaller auxiliary array. Suppose that the subarray原创 2019-07-18 14:36:31 · 161 阅读 · 0 评论 -
Deques and Randomized Queues
Deques and Randomized QueuesDeque.java双向队列,因为要求所有操作的复杂度为O(1)O(1)O(1),采用双向链表进行实现。import java.util.Iterator;import java.util.NoSuchElementException;public class Deque<Item> implements Iterab...原创 2019-07-11 12:37:45 · 255 阅读 · 0 评论 -
Interview Questions: Analysis of Algorithms
Analysis of Algorithms3-SUM in quadratic time. Design an algorithm for the 3-SUM problem that takes time proportional to n2n^2n2 in the worst case. You may assume that you can sort the n integers in...原创 2019-07-01 09:43:23 · 307 阅读 · 0 评论 -
Interview Questions: Elementary Sorts
Elementary SortsIntersection of two sets. Given two arrays原创 2019-07-15 14:25:35 · 218 阅读 · 0 评论 -
Interview Questions: Stacks and Queues
Interview Questions: Stacks and QueuesQueue with two stacks. Implement a queue with two stacks so that each queue operations takes a constant amortized number of stack operations.利用两个栈来实现队列,具体方法为:...原创 2019-07-04 16:44:55 · 339 阅读 · 0 评论 -
Interview Questions: Union-Find
Interview Questions: Union-FindSocial network connectivity. Given a social network containing nn members and a log file containing mm timestamps at which times pairs of members formed friendships, d...原创 2019-07-04 12:41:13 · 247 阅读 · 0 评论 -
Interview Questions: Minimum Spanning Trees
Minimum Spanning TreesBottleneck minimum spanning tree. Given a connected edge-weighted graph, design an efficient algorithm to find a minimum bottleneck spanning tree. The bottleneck capacity of a ...原创 2019-09-12 15:27:16 · 239 阅读 · 0 评论