Cracking the coding interview
文章平均质量分 65
「已注销」
这个作者很懒,什么都没留下…
展开
-
Q9.1 Ruan upp a staircase
Q: A child is running up a staircase with n steps, and can hop either 1 step , 2 steps, or 3 steps at a time. Implement a methon to count how mant possible ways the child can run up the staris.A:斐波那原创 2015-01-25 10:25:54 · 423 阅读 · 0 评论 -
Q4.7 Find commen ancestor
Q: Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing additional nodes in a data structure. NOTE: This is not necessarily a binary sear原创 2015-01-21 20:19:57 · 331 阅读 · 0 评论 -
Q5.2 Print the binary representation
Q:Given a real number between 0 and 1 (eg.,0.72) that is passed in as a string, print the binary representation.If the number can not be represented accurately in binary with 32 characters, print “E原创 2015-01-21 20:58:15 · 341 阅读 · 0 评论 -
Q5.1 Set Bits
Q:You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e.g., M becomes a substring of N located at i and start原创 2015-01-21 20:53:14 · 452 阅读 · 0 评论 -
Q4.8 To decide if T2 is a subtree of T1
Q:You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1A:DFS.T1当前节点是否与T2的根节点相等。如果不相等,查看T2是不是T原创 2015-01-21 20:27:56 · 976 阅读 · 0 评论 -
Q3.6 sort a stack in ascending order
Q:Write a program to sort a stack in ascending order. You should not make any assumptions about how the stack is implemented. The following are the only functions that should be used to write this p原创 2015-01-19 23:21:22 · 448 阅读 · 0 评论 -
Q4.6 find the ‘next’ node
Q: Write an algorithm to find the ‘next’ node (i.e., in-order successor) of a given node in a binary search tree where each node has a link to its parent.A: 中序遍历的后继节点。 即找到最小的大于当前节点值的节点。如果当前节点存在右原创 2015-01-20 22:21:19 · 347 阅读 · 0 评论 -
Q3.5 Implement a MyQueue class which implements a queue using two stacks.
Q:Implement a MyQueue class which implements a queue using two stacks.A:一个栈sin用于进,一个栈sout用于出。push:直接push进sin即可pop: 如果sout不空,直接pop,否则将sin中的元素都压入sout之后再sout.popfront: 同pop,只不过不pop,而是topback:同f原创 2015-01-19 23:16:31 · 516 阅读 · 0 评论 -
Q4.5 Check if a binary search is BST
Q: Implement a function to check if a binary is a BSTA: DFS根据平衡二叉树的定义,对于根节点root来说:1、如果左孩子存在,那么root的值大于其左孩子的值2、如果右孩子存在,那么root的值小于其右孩子的值3、其左子树和右子树都是BST(递归判定)满足上面的三个条件,该二叉树是BST,否则不是#in原创 2015-01-20 21:57:09 · 422 阅读 · 0 评论 -
Q3.7 Create a data structures to hold dogs and cats.(待续)
Q: An animal shelter holds only dogs and cats, and operates on a strictly "first in, first out" basis. People must adopt either the "oldest" (based on arrival time) of all animals at the shelter, or t原创 2015-01-19 23:31:06 · 349 阅读 · 0 评论 -
Q.3.4 To solve the problem of hanoi
Q: In the classic problem of the Towers of Hanoi, you have 3 rods and N disks of different sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending order of size from原创 2015-01-19 23:04:12 · 313 阅读 · 0 评论 -
Q4.9 Find all paths which sum to a given value
Q:You are given a binary tree in which each node contains a value. Design an algorithm to print all paths which sum up to that value. Note that it can be any path in the tree - it does not have to s原创 2015-01-21 20:39:16 · 419 阅读 · 0 评论 -
Q5.3 Print the next largest and smallest number。 二进制表示中1的个数统计
Q:Given an integer, print the next smallest and next largest number that have the same number of 1 bits in their binary representation.A:思路1:暴力搜索。统计数字的二进制表示中1的位数。统计方法参考http://blog.csdn.net/steedho原创 2015-01-22 20:28:17 · 522 阅读 · 0 评论 -
Q5.4 ((n & (n-1)) == 0).
Q:Explain what the following code does: ((n & (n-1)) == 0).A: 其作用是判断n是否为0 或者是2的整数幂因为如果n为2的m次幂的话,那么其二进制表示中只有第m位为1,其余全部为0(从0位开始)原创 2015-01-22 21:06:16 · 606 阅读 · 0 评论 -
Q 9.7 paint fill
Q: Implement the “paint fill” function that one might see on many image editing programs. That is, given a screen (represented by a 2-dimensional array of Colors), a point, and a new color, fill in原创 2015-01-25 20:57:01 · 362 阅读 · 0 评论 -
Q9.5 Write a method to compute all permutations of a string
Q:Write a method to compute all permutations of a stringA:为了处理方便,下面的代码不是求一个string的全排列,而是求数组的全排列,本质上是一致的1、如果数据不存在重复。例如[1,2,3]想要得到全排列,就是要逐位交换。即1与自身交换,然后加上[2,3]的全排列,这只是以1开始的序列;接下来1与2交换,得[2,1,3],如法炮原创 2015-01-25 15:29:12 · 433 阅读 · 0 评论 -
Q5.8 DrawHorizontalLine
Q: A monochrome screen is stored as a singal array of bytes, allowing eight consecutive pixels to be stored in one byte. The screen has width w, where w is a divisible by 8 (that is, no byte will be s原创 2015-01-25 09:18:57 · 543 阅读 · 0 评论 -
Q9.8 Combine Sum
Q:Given an infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents) and pennies (1 cent), write code to calculate the number of ways of representing n cents.A:思路:DFS例如要构造100原创 2015-01-25 22:26:01 · 360 阅读 · 0 评论 -
Q9.3 Find the magic index
Q: A magic index in an array A[1...n-1] is defined to be an index such that A[i] = i. Given a sorted array of distince integers, write a methon to find a magic index, if one exits, in array A。FOLLOW原创 2015-01-25 13:31:17 · 406 阅读 · 0 评论 -
Q9.6 Generate Parentheses
Q: Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-pairs of parentheses.EXAMPLE:input: 3 (e.g., 3 pairs of parentheses)output: ((())), ((原创 2015-01-25 16:11:12 · 374 阅读 · 0 评论 -
Q9.4 Write a method that returns all subsets of a set.
Q:Write a method that returns all subsets of a set.A:DFS.#include #include #include using namespace std;void dfs(const vector &s, int start, vector &path, vector > &res) { if (start == s.si原创 2015-01-25 14:27:40 · 462 阅读 · 0 评论 -
Q5.6 swap odd and even bits
Q:Write a program to swap odd and even bits in an integer with as few instructions as possible (e.g., bit 0 and bit 1 are swapped, bit 2 and bit 3 are swapped, etc).A:将奇数位和偶数位提取出来,再移位取或就可以了#incl原创 2015-01-22 21:32:20 · 420 阅读 · 0 评论 -
Q5.5 convert integer A to integer B
Q: Write a function to determine the number of bits required to convert integer A to integer B.Input: 31, 14Output: 2A: 统计A^B二进制中1的个数就即可。#include using namespace std;int count_o原创 2015-01-22 21:08:26 · 423 阅读 · 0 评论 -
Q5.7 Find the missing number
Q: An array A[1…n] contains all the integers from 0 to n except for one number which is missing. In this problem, we cannot access an entire integer in A with a single operation. The elements of A a原创 2015-01-22 23:14:16 · 601 阅读 · 0 评论 -
Q4.3 Convert a sorted array to a binary tree
Q:Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal height.A:DFS问题 找到array的中点,作为树的根节点。start~mid-1构成其左子树, mid+1~end构成其右子树。这样不断递归下去。#include #in原创 2015-01-20 21:02:57 · 260 阅读 · 0 评论 -
Q4.2 Given a directed graph, design an algorithm to find out whether there is a route.
Q: Given a directed graph, design an algorithm to find out whether there is a route between two nodes.A: 采用bfs, 同时利用哈希表保存当前节点是否已经被访问了。如果节点i 和节点j 相连,并且j没有被访问过,那么节点j入栈,说明节点j可能是所求路径上的点。就这样遍历,直到栈为空或原创 2015-01-20 20:56:51 · 578 阅读 · 0 评论 -
1.2 Write code to reverse a C-Style String.
Write code to reverse a C-Style String.#include#includeusing namespace std;void swap(char &a, char &b){ a = a^b; b = a^b; a = a^b;}void reverse(char *str) { int n = strl原创 2015-01-16 22:02:14 · 455 阅读 · 0 评论 -
1.1 Implement an algorithm to determine if a string has all unique characters.
Implement an algorithm to determine if a string has all unique characters.What if you can not use additional data structures?思路:首先,字符串是ascii编码还是unicode编码,这会影响记录的数组。 用bool ch[256] 记录所有的字符,初始化为fa原创 2015-01-16 21:10:36 · 665 阅读 · 0 评论 -
Q1.7 set Matrix zeroes
Q:Write an algorithm such that if an element in an MxN matrix is 0,its entire row and column is set to 0.A: 通过遍历,可以用(m+n) 哈希表来储存每一列和每一行是否为0, 再根据哈希表的记录,再次遍历置0;为了节省空间,可以在上面的思路基础之上进行优化:1、首先遍历矩阵的第一行原创 2015-01-17 10:36:23 · 542 阅读 · 0 评论 -
Q1.6 To rotate a image by 90 degress.
Q: Given an image represented by an NxN matrix, where each pixel in theimage is 4 bytes, write a method to rotate the image by 90 degrees.Can you do this in place?A:思路一:第一步交换主对角线两侧的对称元素,第二步交换第i行原创 2015-01-17 09:51:06 · 391 阅读 · 0 评论 -
Q1.5 String Compression
Implement a method to perform basic string compression using the counts of repeated characters.eg: aabcccccaaa -----> a2b1c5a3如果压缩后的字符串不比原来的字符串小,那么返回原来的字符串。原创 2015-01-16 23:21:46 · 440 阅读 · 0 评论 -
Q1.4 Write a method to replace all spaces in a string with ‘%20’.
思路:1、遍历, 原来的字符串长度len, 记录空格个数cnt, 则最后的字符串长度len+2*cnt。2、从后往前遍历,遇到空格将其替换为"%20",否则原样拷贝#include #include using namespace std;void replace(char *c){ if(c == NULL) return; int len = strlen(原创 2015-01-16 23:17:57 · 500 阅读 · 0 评论 -
Q1.3
Given two strings, write a method to decide if one is a perutation of the other.思路1:排序,在比较思路2:哈希表记录#include#include#include using namespace std;bool permutation1(string s, string t) {原创 2015-01-16 22:18:30 · 288 阅读 · 0 评论 -
Q9.11 count the number of ways of parenthesizing the expression
Q:Given a boolean expression consisting of the symbols 0,1, &, /, and A, and a desired boolean result value result, implement a function to count the number of ways of parenthesizing the expression su原创 2015-01-28 19:45:53 · 448 阅读 · 0 评论 -
Q9.10 To build the tallest stack
Q:You have a stack of n boxes, with widths w., heights l\ and depths dr The boxes cannot be rotated and can only be stacked on top of one another if each box in the stack is strictly larger than the b原创 2015-01-28 17:32:16 · 500 阅读 · 0 评论 -
Q9.9 N queens
Q: Write an algorithm to print all ways of arranging eight queens on a chess board so that none of them share the same row, column or diagonal.A: 经典的8皇后问题。DFS#include #include #include using原创 2015-01-25 23:10:28 · 773 阅读 · 0 评论 -
Q1.8 Check if s2 is a rotation of s1
Q: Assume you have a method isSubstring which checks if one word is asubstring of another. Given two strings, s1 and s2, write code tocheck if s2 is a rotation of s1 using only one call to isSubstring原创 2015-01-17 11:14:32 · 769 阅读 · 0 评论 -
Q3.2 Stack with Min
Q; How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element? Push, pop and min should all operate in O(1) time.A: 用两个栈s1, s2, s1正常原创 2015-01-18 23:26:04 · 333 阅读 · 0 评论 -
Q2.1 Write code to remove duplicates from an unsorted linked list.
Q:Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this problem if a temporary buffer is not allowed?A:用哈希表记录某一值是否已经存在,下面的程序只对小于100的数有效。1、当访问的链原创 2015-01-17 12:35:21 · 364 阅读 · 0 评论 -
Q4.1 Implement a function to check if a tree is balanced
Q: Implement a function to check if a tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that no two leaf nodes differ in distance from the root by mor原创 2015-01-20 20:37:51 · 402 阅读 · 0 评论