题目原文: Design an algorithm to perform an inorder traversal of a binary search tree using only a constant amount of extra space. public void traverse(BST<Key,Value> bst) { traverse(bst.root.left, bst.root); } private void traverse(Node current, Node p…
第二周课程的Elementray Sorts部分练习测验Interview Questions的第3题荷兰国旗问题很有意思.题目的原文描述如下: Dutch national flag. Given an array of n buckets, each containing a red, white, or blue pebble, sort them by color. The allowed operations are: swap(i,j): swap the pebble in buc…
题目原文: Given two integer arrays of size n , design a subquadratic algorithm to determine whether one is a permutation of the other. That is, do they contain exactly the same entries but, possibly, in a different order. 本质上就是求两个数组排序后是否相等,鉴于本节课学的是选择.插入.…
题目原文: Given two arrays a[] and b[], each containing n distinct 2D points in the plane, design a subquadratic algorithm to count the number of points that are contained both in array a[] and array b[]. 题目的目标就是计算重复point的个数,很简单,代码如下 import java.awt.Poin…
题目原文: Stack with max. Create a data structure that efficiently supports the stack operations (push and pop) and also a return-the-maximum operation. Assume the elements are reals numbers so that you can compare them. 分析: 该题目要求在实现正常stack的push和pop操作外,还…
题目原文: Implement a queue with two stacks so that each queue operations takes a constant amortized number of stack operations. 题目要求用栈实现队列的所有操作. package week2; import java.util.Stack; /** * Queue with two stacks. Implement a queue with two stacks so tha…