17.14 Oh, no! You have just completed a lengthy document when you have an unfortunate Find/Replace mishap. You have accidentally removed all spaces, punctuation, and capitalization in the document. A sentence like "I reset the computer. It still didn…
Oh, no! You have just completed a lengthy document when you have an unfortu- nate Find/Replace mishap. You have accidentally removed all spaces, punctuation, and capitalization in the document. A sentence like "I reset the computer. It still didn't b…
17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏,有下面几点需要考虑: 1. 判断是否能赢hasWon函数是调用一次还是多次,如果是多次,我们可能为了优化而需要加入一些预处理. 2. 井字棋游戏通常是3x3的大小,我们是否想要实现NxN的大小? 3. 我们需要在代码紧凑,执行速度和代码清晰之间做出选择. #include <iostream> #…
17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other nodes. The data structure BiNode could be used to represent both a binary tree (where nodel is the left node and node2 is the right node) or a doubly link…
17.12 Design an algorithm to find all pairs of integers within an array which sum to a specified value. 这道题实际上跟LeetCode上的Two Sum很类似,但是不同的是,那道题限定了只有一组解,而这道题说可以有很多组符合要求的解,那么我们先来看一种使用哈希表的解法,这种解法的时间复杂度是O(n),空间复杂度是O(1),思路是用哈希表建立每个数字和其下标之间的映射,遍历整个数字,如果targ…
17.11 Implement a method rand7() given rand5(). That is, given a method that generates a random number between 0 and 4 (inclusive), write a method that generates a random number between 0 and 6 (inclusive). 这道题说给了我们一个rand5()函数,可以生成0到4之间的随机数,让我们写一个函数r…