自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(220)
  • 收藏
  • 关注

原创 宜信电面记录

此次电面主要是先自我介绍,然后针对简历上面的项目问了一些基本的问题。考了两个算法题。第一个是句子翻转, eg "this is right" ---> "right is this"思路:可以先把字符串整体反转,然后再针对每个单词分别反转。第二个是数组操作。给定a1, a2, ..., an.使其输出为 b1 = b3 这道题也不难,直接相邻数值进行比较就可以。面试官人很n

2015-04-28 23:32:06 1073

原创 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 412

原创 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 472

原创 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 742

原创 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 337

原创 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 345

原创 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 352

原创 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 413

原创 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 440

原创 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 383

原创 Q9.2 Find paths

Q: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 UPImagin

2015-01-25 10:49:27 418

原创 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 397

原创 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 511

原创 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 577

原创 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 387

原创 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 397

原创 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 586

原创 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 497

原创 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 318

原创 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 429

原创 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 402

原创 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 955

原创 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 307

原创 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 322

原创 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 404

原创 Q 4.4 creates a linked list of all the nodes at each depth

Q: Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (i.e., if you have a tree with depth D, you’ll have D linked lists).A:思路1:逐层建立链表。遍

2015-01-20 21:18:47 326

原创 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 240

原创 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 554

原创 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 380

原创 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 331

原创 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 432

原创 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 493

原创 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 293

原创 Q.3.3 SetOfStacks

Q: 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. Impl

2015-01-19 19:47:15 242

原创 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 308

原创 Q3.1 Describe how you could use a single array to implement three stacks

Q:Describe how you could use a single array to implement three stacksA:思路一:比较直观的想法是将一个数组分为三部分。将数组平分三部分, 每个部分维护一个栈顶指针。对特定的栈进行操作只要用栈顶指针加上偏移量就可以。思路二:思路一可能会浪费大量的空间。所以不均分数组为三部分。定义一种数据类型,可以记录当前元素值和上一个

2015-01-18 23:20:30 622

原创 Q2.7 Check if a list is a palindrome

Q:Check if a list is a palindromeA: 将链表的后半部分反转,然后和前半部分分别比较#include using namespace std;struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; ListNo

2015-01-18 13:56:28 321

原创 Q2.6 Find the Beginning of the loop

Q:Given a circular linked list, implement an algorithm which returnsnode at the beginning of the loop.A: 思路1:可以用哈希表记录每一个节点,遍历链表,通过哈希表记录判断,如果当前节点已经被访问过,那么有环。如果遍历结束,那么没有loop思路2:1、双指针查看是否有环。快

2015-01-18 13:53:53 277

原创 Q2.5 Add Two Numbers

Q: 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 wat the head of the list. Write a func

2015-01-18 13:43:51 225

原创 Q2.4 Partition List

Q:Write code to partiton a linked list around a value x, such that all nodes less than x come before all nodes greater than or equal to x.A:双链表,其中一个记录小于x的置的节点,剩下的节点记录在第一个链表下,最后将第一个链表的尾节点指向第二个链表的head

2015-01-18 13:40:23 216

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除