面试题
stevemarbo
这个作者很懒,什么都没留下…
展开
-
第6题 约瑟夫环问题
题目: n个数字 (0,1,2,3, ... , n-1) 形成一个圆圈,从数字0开始,每次从这个圆圈中删除第m个数字。 第一个为当前数字本身,第二个为当前数字的下一个数字。 当一个数字删除后,从被删除数字的下一个继续删除第m个数字 此题目的另一种表述是:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人原创 2012-02-23 19:21:54 · 1458 阅读 · 3 评论 -
第10题 把二元查找树转换成排序的双向链表
题目:把二元查找树转换成排序的双向链表 要求:不能创建任何新的节点,只调整指针的指向 10 / \ 6 14 / \ / \ 4 8 12 16 转成双向链表 4=6=8=12=14=16 关于此题, 有一篇非常详细的文档,把这个问题讲述的非常清楚,英文好的朋友可以看原创 2012-02-27 17:14:09 · 649 阅读 · 0 评论 -
第13题 check rotation string
此题来自cracking the coding interview,这本书是讲如何准备程序员面试的,非常不错 免积分下载: http://download.csdn.net/detail/stevemarbo/4098275 assume you have a method isSubstring which checks if one word is a sub原创 2012-02-28 23:28:53 · 1249 阅读 · 0 评论 -
第14题 find longest common continuous substring
Write Program to find longest common contiguous intersection from 2 lists provided to the function. Example: list1: abcrfghwetf list2: abrfghwwetxyab Longest common intersection here is:原创 2012-03-01 20:57:22 · 863 阅读 · 0 评论 -
第15题 To Check the given linked list is palindrome or not
检查链表是否是一个palindrome? 什么是palindrome? 举个例子 12344321 这就是个palindrome,开头的1和结尾的1对应,第二位的2和倒数第二位的2对应 我查到一个不错的方法,大家可以参考一下,思路很好,利用了栈FIFO的性质 public static boolean isPalindrome(Node head) { Node原创 2012-03-03 15:36:46 · 1827 阅读 · 0 评论 -
第16题 reverse every K elements of a linked list
Write a program to reverse every K elements of a linked list. Example: K = 3 1->2->3->4->5->6->NULL output: 3->2->1->6->5->4-NULL struct node *reverse (struct node *head, int k) {原创 2012-03-03 17:05:30 · 800 阅读 · 0 评论 -
第17题 去除一个字符串中的连续空格
比如,一个字符串是: My name is Steve. 这个字符串里的单词之间的空格个数不一样,去除多余的空格,使两个单词之间只有一个空格 #include #include int main() { char* s = "This is an Example"; printf("%s\n",s); int i=原创 2012-03-04 06:34:37 · 869 阅读 · 0 评论 -
第18题 比较两个整数比特位不同的数量
此题来自 cracking the coding interview write a function to determine the number of of bits required to convert integer A to integer B input: 31, 14 output: 2 #include int count(int a, int原创 2012-03-07 17:55:54 · 1042 阅读 · 0 评论 -
第9题 求整数的二进制表达式中1的个数
题目:输入一个整数,求该整数的二进制表达式中有多少个1 例如,输入10,由于其二进制表示为1010,有两个1,因此输出2 此题考察位运算,微软曾经用过这道题 xxxx1000 & (xxxx1000-1) = xxxx0000 #include int count1(int n) { int c=0; while(n!=0) { n = n&(n-1)原创 2012-02-24 09:02:25 · 735 阅读 · 0 评论 -
第2题 求子数组最大和
求子数组的最大和 题目:输入一个整形数组,数组里有正数也有负数 数组中连续的一个或多个数组组成一个子数组,每个子数组都有一个和 求所有子数组的最大值,要求时间复杂度为O(n) 例如输入的数组为 1, -2, 3, 10, -4, 7, 2, -5 最大的子数组为 3, 10, -4, 7, 2 因此输出为该子数组的和 18 #include i原创 2012-02-23 09:54:24 · 446 阅读 · 0 评论 -
第4题 颠倒一个句子中的词的顺序
题目: 颠倒一个句子中的词的顺序,比如: I am a student 颠倒后变成:student a am I 此题紧接着第3题,不过要把第3题的函数做一定修改 #include #include char* reverse(char* str, int n) { int end = n - 1; int start = 0; while(start原创 2012-02-23 10:52:27 · 3793 阅读 · 0 评论 -
第3题 reverse字符串
颠倒一个字符串。优化时间,优化空间。 此题也就是字符串倒置,类似的问题经常在面试中出现,我在stackoverflow上查到一个很不错的方法 #include #include char* reverse(char* str) { int end = strlen(str) - 1; int start = 0; while(start<end) {原创 2012-02-23 10:29:29 · 817 阅读 · 0 评论 -
第7题 倒置一个链表
题目:利用递归倒置一个链表 此题非常常见,因为很多公司在出面试题的时候,会考察面试人员的数据结构知识和算法知识,而有关链表的题是最具代表性的了。 这种题目不是非常难,适合做面试题,但又不简单,如果不提前做好准备,真正到了面试时,很难能做出来 #include #include #include #include struct node { int data;原创 2012-02-24 02:18:56 · 693 阅读 · 0 评论 -
第12题 判断两个链表是否相交
题目:判断两个链表是否相交 分析:如果被问道此问题,一定要分两种情况回答,第一种是链表不存在环,第二种就是存在环的情况 1. 先判断带不带环 2. 如果都不带环,就判断尾节点是否相等 3. 如果都带环,判断一链表上俩指针相遇的那个节点,在不在另一条链表,如果在,则相交,如果不在,则不相交 struct node { int data; struct node*原创 2012-02-27 18:34:38 · 755 阅读 · 0 评论 -
第8题 把字符串转换成整数输出
题目:输入一个表示整数的字符串,把该字符串转换成整数并输出 例如,输入字符串“123”,则输出整数123,输入字符串“-456”,则输出整数-456,输入字符串“a123”,则输出 illegal number #include #include int strtoint(char* str) { int neg=1; char* p = str; if(*原创 2012-02-24 08:49:14 · 754 阅读 · 0 评论 -
第1题 在排序数组中查找和为某个指定数的两个元素之和
题目:输入一个已经按照升序排序过得数组和一个数字,在数组中查找两个数,使得他们的和正好是输入的那个数字 要求时间复杂度是O(n),如果有多对数字的和等于输入的数字,输出任意一对即可。 例如输入数组 1,2,4,7,11,15和数字15 由于4+11 = 15,因此输出4和11 #include void find2Number(int a[], int n, in原创 2012-02-23 05:37:14 · 1057 阅读 · 0 评论 -
第11题 求链表倒数第k个节点
题目:输入一个单向链表,输出该链表中倒数第k个节点 链表的倒数第0个节点为链表的尾指针 设置两个指针 p1,p2,首先,p1和p2都指向head,然后p2向前走k步,这样p1和p2之间就间隔k个节点,最后p1和p2同时向前移动,直至p2走到链表末尾 struct node { int data; struct node* next; } struct node* fi原创 2012-02-27 17:55:25 · 434 阅读 · 0 评论 -
第5题 在一个字符串中找到第一个只出现一次的字符
题目:在一个字符串中找到第一个只出现一次的字符。 例如输入 abaccdeff ,则输出b 此题据说是2006年google的一道笔试题 假设全都是ASCII码,也就是说一个字符对应一个相应的整数 #include #include char first_single_char(char* str) { int ascii[255]; memset(原创 2012-02-23 18:05:18 · 612 阅读 · 2 评论 -
第19题 在二叉查找树中找到两个结点的最低公共祖先 Lowest Common Ancestor
本文来自《 programming interviews exposed》一书 题目: Given the value of two nodes in a binary search tree, find the lowest common ancestor. You may assume that both values already exist in the tree. The fun原创 2012-03-18 05:44:35 · 2092 阅读 · 0 评论