Cracking the coding interview
cfanrCoder
对贡献有激情,对回报有信心!
展开
-
Cracking the coding interview--Q1.2
题目:原文:Write code to reverse a C-Style String. (C-String means that “abcd” is represented as five characters, including the null character.)译文:写代码反转一个C风格的字符串(C风格的字符串是如"abcd"需要五个字符表示,包括尾部的结束符)原创 2014-01-21 21:53:55 · 1444 阅读 · 0 评论 -
Cracking the coding interview--Q1.3
题目:原文:Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer. NOTE: One or two additional variables are fine. An extra copy of the ar原创 2014-01-22 22:19:05 · 1520 阅读 · 0 评论 -
Cracking the coding interview--Q1.4
题目原文:Write a method to decide if two strings are anagrams or not.译文:写一个方法判断两个字符串是否是变位词而成的。解答所谓变位词(anagrams)就是字符串的组成的字符都是一样,只是位置不同而已,如:abcdd和dabdc就是一组变位词。方法1:将两个字符串重新按照ascii值顺序排列,在比较是原创 2014-01-23 17:27:41 · 1469 阅读 · 0 评论 -
Cracking the coding interview--Q1.5
题目原文:Write a method to replace all spaces in a string with ‘%20’.译文:写一个方法用'%20'代替一个字符串中的所有空格。解答方法一:直接用一个String类型的变量将字符串中的非空格字符和‘%20’用加法串起来;方法二:遍历一次字符串,数出空格数,然后开一个足够大的字符串空间,将字符一个个读进去;原创 2014-01-24 00:29:39 · 1378 阅读 · 0 评论 -
Cracking the coding interview--Q1.6
题目原文:Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?译文:给出一个原创 2014-01-26 22:13:58 · 1877 阅读 · 0 评论 -
Cracking the coding interview--Q1.7
题目原文:Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.译文:写一个算法使一个MxN的矩阵中出现0的元素的行和列都设为0.解答遍历一遍矩阵,将出现0的元素用两个数组将其行列号存储下来,或开一个行数组row和列数原创 2014-01-26 23:39:29 · 1864 阅读 · 1 评论 -
Cracking the coding interview--Q1.8
题目原文:Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isS原创 2014-01-27 16:40:33 · 1248 阅读 · 0 评论 -
Cracking the coding interview--Q1.1
题目:原文:Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?译文:实现一个算法判断一个字符串中的字符是否唯一的,不能使用额外的数据结构。解答:思路:首先应该思考构成字符串的原创 2014-01-20 21:52:58 · 2321 阅读 · 0 评论 -
Cracking the coding interview--Q2.2
题目原文:Implement an algorithm to find the nth to last element of a singly linked list.译文:实现一个算法找出一个单链表的倒数第n个元素。解答方法原创 2014-02-04 19:45:34 · 1202 阅读 · 0 评论 -
Cracking the coding interview--Q3.2
题目原文: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.译文:设计一个栈,除了原创 2014-02-05 18:01:42 · 1252 阅读 · 0 评论 -
Cracking the coding interview--Q3.3
题目原文:Imagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, in real life, we would likely start a new stack when the previous stack exceeds some threshold. Im原创 2014-02-05 22:15:02 · 1273 阅读 · 0 评论 -
Cracking the coding interview--Q2.3
题目原文:Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node.EXAMPLEInput: the node ‘c’ from the linked list a->b->c->d->eResult: nothin原创 2014-02-04 21:04:24 · 1266 阅读 · 0 评论 -
Cracking the coding interview--Q2.5
题目原文:Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.DEFINITIONCircular linked list: A (corrupt) linked list in which a node’s next pointer原创 2014-02-05 11:47:49 · 1193 阅读 · 0 评论 -
Cracking the coding interview--Q3.5
题目原文:Implement a MyQueue class which implements a queue using two stacks.译文:用两个栈实现一个队列MyQueue。解答方法原创 2014-02-06 22:27:13 · 1319 阅读 · 0 评论 -
Cracking the coding interview--Q2.4
题目原文:You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1’s digit is at the head of the list. Write a原创 2014-02-04 23:40:31 · 1194 阅读 · 0 评论 -
Cracking the coding interview--Q3.1
题目原文:Describe how you could use a single array to implement three stacks.译文:描述怎样用一个单独的数组来实现三个栈。解答解法一:将数组划分成3等份,每一份独立的用来实现堆栈。*第一个堆栈:从 0 至 n/3*第二个堆栈:从 n/3 至 2n/3*第三个堆栈:从2n/3 至原创 2014-02-05 15:55:36 · 1347 阅读 · 0 评论 -
Cracking the coding interview--Q2.1
题目原文:Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this problem if a temporary buffer is not allowed?译文:写代码从一个无序的链表移除重复的项更进一步,如果不用临时缓存,怎样解原创 2014-02-04 15:01:49 · 1297 阅读 · 0 评论 -
Cracking the coding interview--Q3.4
题目原文: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 fr原创 2014-02-05 22:26:05 · 1266 阅读 · 0 评论 -
Cracking the coding interview--Q3.6
题目原文: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原创 2014-02-07 23:25:14 · 1277 阅读 · 0 评论 -
Cracking the coding interview--Q4.1
题目原文: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原创 2014-02-09 00:02:30 · 1667 阅读 · 1 评论 -
Cracking the coding interview--Q8.1
题目原文:Write a method to generate the nth Fibonacci number.译文:写一个方法产生第n个斐波那契数。解答斐波那契数列(又称黄金分割数列)指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...特别指出:0是第0项,不是第1项。这个数列从第二项开始原创 2014-02-23 11:24:38 · 1296 阅读 · 0 评论 -
Cracking the coding interview--Q4.2
题目原文:Given a directed graph, design an algorithm to find out whether there is a route between two nodes.译文:给一个有向图,设计一个算法去找出两个结点间是否有路径.解答方法原创 2014-02-10 10:01:18 · 1293 阅读 · 0 评论 -
Cracking the coding interview--Q5.6
题目原文: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).译文:写程序用尽可能少的指令实现对一个整数的原创 2014-02-22 17:07:26 · 1333 阅读 · 0 评论 -
Cracking the coding interview--Q5.7
题目原文: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原创 2014-02-22 23:30:13 · 1526 阅读 · 0 评论 -
Cracking the coding interview--Q4.3
题目原文:Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal height.译文:给一个已经排好序(增序)的数组,写一个算法创建一个具有最小高度二叉树。解答要使二叉树高度最小,则其左右结点数应相当。那么将数组对半分,以中间数为父结原创 2014-02-11 21:18:08 · 1200 阅读 · 0 评论 -
Cracking the coding interview--Q4.4
题目原文:Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (eg, if you have a tree with depth D, you’ll have D linked lists).译文:给一个二叉查找树,原创 2014-02-11 22:20:48 · 1240 阅读 · 0 评论 -
Cracking the coding interview--Q8.2
题目原文:Imagine a robot sitting on the upper left hand corner of an NxN grid. The robot can only move in two directions: right and down. How many possible paths are there for the robot?FOLLOW UPI原创 2014-02-25 11:04:42 · 1386 阅读 · 0 评论 -
Cracking the coding interview--Q8.3
题目原文:Write a method that returns all subsets of a set.译文:写一个方法返回一个集合的所有子集合。解答对于一个集合,它的总共子集数为2^n(包括空集)。对于S(5)={1,2,3,4,5},则S(5)的子集数可以写为:5与S(4)的子集的组合+S(4)的子集,得递归算法如下: public static Linke原创 2014-02-25 13:08:27 · 1812 阅读 · 0 评论 -
Cracking the coding interview--Q4.5
题目原文:Write an algorithm to find the ‘next’ node (e.g., in-order successor) of a given node in a binary search tree where each node has a link to its parent.译文:给定一个二叉查找树的结点,写一个算法去找“next”结点(即中原创 2014-02-14 22:33:25 · 1164 阅读 · 0 评论 -
Cracking the coding interview--Q5.1
题目原文: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 st原创 2014-02-15 21:45:14 · 1384 阅读 · 0 评论 -
Cracking the coding interview--Q4.6
题目原文: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原创 2014-02-14 23:51:15 · 1270 阅读 · 0 评论 -
Cracking the coding interview--Q8.4
题目原文:Write a method to compute all permutations of a string.译文:写一个方法返回一个字符串的所有排列。解答显然对于一个长度为n的串,它的全排列共有A(n, n)=n!种,由递归的方法解,若字符串是“abc”,函数名为recursionPermu,由不同的递归思路分析。解法一:将第0个字符a取出,递归调用re原创 2014-02-26 21:20:16 · 1270 阅读 · 0 评论 -
Cracking the coding interview--Q8.5
题目原文: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: ()()(), ()(())原创 2014-02-27 11:14:25 · 1486 阅读 · 0 评论 -
Cracking the coding interview--Q4.7
题目原文: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 T1.译文:有两个非常大的二叉树:T1,有上百万个结点,T2,有上百个原创 2014-02-15 14:27:07 · 1347 阅读 · 0 评论 -
Cracking the coding interview--Q4.8
题目原文: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原创 2014-02-15 20:35:14 · 1667 阅读 · 1 评论 -
Cracking the coding interview--Q8.6
题目原文: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原创 2014-02-28 11:55:42 · 1425 阅读 · 0 评论 -
Cracking the coding interview--Q5.2
题目原文:Given a (decimal - e.g. 3.72) number that is passed in as a string, print the binary representation. If the number can not be represented accurately in binary, print “ERROR”译文:给一个字符类型表示原创 2014-02-17 21:49:05 · 1216 阅读 · 0 评论 -
Cracking the coding interview--Q8.7
题目原文: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.译文:给无限个25分,原创 2014-03-01 11:45:49 · 1296 阅读 · 0 评论 -
Cracking the coding interview--Q5.3
题目原文:Given an integer, print the next smallest and next largest number that have the same number of 1 bits in their binary representation.译文:给一个整数,打印两个数,并且他们的二进制表示中的1的个数和这个整数的一样,其中一个是比该数大的数中原创 2014-02-19 12:05:54 · 1355 阅读 · 0 评论 -
Cracking the coding interview--Q5.4
题目原文:Explain what the following code does: ((n & (n-1)) == 0).译文:解释下面代码的作用:((n&(n-1))==0)解答分析:首先该代码是一个位运算的式子,两个相邻的数作按位与运算得到0,而当两个二进制的各位不同时,作按位与运算才会等于0,所以只有当n为2的整数次幕时,比它小1的数的二进制的各位才会与它的二进原创 2014-02-19 17:31:47 · 1249 阅读 · 0 评论