Hawstein's Blog

Make something people love

Cracking the coding interview--Q8.8

题目 原文: 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. 译文: 经典的八皇后问题,即在一个8*8的棋盘上放8个皇后,使得这8个皇后无法互相攻击...

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...

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,...

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: ((())), ((...

Cracking the coding interview--Q8.4

题目 原文: Write a method to compute all permutations of a string 译文: 写一个函数返回一个串的所有排列 解答 对于一个长度为n的串,它的全排列共有A(n, n)=n!种。这个问题也是一个递归的问题, 不过我们可以用不同的思路去理解它。为了方便讲解,假设我们要考察的串是”abc”, 递归函数名叫permu。 思路一: ...

Cracking the coding interview--Q8.3

题目 原文: Write a method that returns all subsets of a set. 译文: 写一个函数返回一个集合中的所有子集。 解答 对于一个集合,它的子集一共有2^n 个(包括空集和它本身)。它的任何一个子集, 我们都可以理解为这个集合本身的每个元素是否出现而形成的一个序列。比如说, 对于集合{1, 2, 3},空集表示一个元素都没出现,对应{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 ...

Cracking the coding interview--Q8.1

题目 原文: Write a method to generate the nth Fibonacci number. 译文: 写一个函数来产生第n个斐波那契数。 解答 斐波那契数列的定义如下: 1 2 f(1) = f(2) = 1; f(n) = f(n-1) + f(n-2); 这个定义是递归的,因此很容易根据以上的定义写出它的递归解法, 由于这个数列的递增速度飞快X...

Cracking the coding interview--Q6.1~Q6.6

题目6.1 原文: Add arithmetic operators (plus, minus, times, divide) to make the following expression true: 3 1 3 6 = 8. You can use any parentheses you’d like. 解答6.1 简单题,就不翻译了。答案:(3 + 1) / (3 / 6...

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 elemen...