随笔分类 - Googl面试CTCI
摘要:【链表】Q:Implement an algorithm to find the nth to last element of a singly linked list .题目:找出链表的倒数第n个节点元素。解答: 方法一:利用两个指针p,q,首先将q往链表尾部移动n位,然后再将p、q一起往后移,那么当q达到链表尾部时,p即指向链表的倒数第n个节点。node* find_nth_to_last(node* head,int n) { if(head==NULL || nnext; } if(n>=0) return NULL; while(p!=NULL && q!=NU
阅读全文
摘要:【链表】Q: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: nothing is returned, but the new linked list looks like a->b->d->e题目:实现算法来删除单链表中的中间节点(只知道指向该节点
阅读全文
摘要:【链表】Q:Write code to remove duplicates from an unsorted linked list FOLLOW UP How would you solve this problem if a temporary buffer is not allowed?题目:编码实现从无序链表中移除重复项。 如果不能使用临时缓存,你怎么编码实现?解答: 方法一:不使用额外的存储空间,直接在原始链表上进行操作。首先用一个指针指向链表头节点开始,然后遍历其后面的节点,将与该指针所指节点数据相同的节点删除。然后将该指针后移一位,继续上述操作。直到该指针移到链表。void ..
阅读全文
摘要:创建链表、往链表中插入数据、删除数据等操作,以单链表为例。 1.使用C语言创建一个链表:typedef struct nd{ int data; struct nd* next; } node; //初始化得到一个链表头节点 node* init(void){ node* head=(node*)malloc(sizeof(node)); if(head==NULL) return NULL; head->next=NULL; return head;} //在链表尾部插入数据void insert(node* head,int data){ if(head==NU...
阅读全文
摘要:【字符串与数组】Q: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 isSubstring (i e , “waterbottle” is a rotation of “erbottlewat”)题目:给定一个能判断一个单词是否为另一个单词的子字符串的方法,记为isSubs
阅读全文
摘要:【字符串与数组】Q: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,就将这个0所在的行和列全部置为0,咋看一下没问题,仔细一想不对啊,这样做后,很有可能操作完后,这个二维数组里就全变成0了。注意了啊,别进了思维误区。 方法二:开辟两个数组,分别用于标记某一行是否出现了0,如果出现
阅读全文
摘要:【字符串与数组】Q: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?题目:假定一幅图像能用NxN的矩阵表示,每个像素是四字节。写一个算法将图像旋转90度,你能否在原地进行操作(也即不分配额外的存储空间)?解答:我们不知道具体该如何操作,但有一点可以肯定的是,需要通过交换一些元素的位置来达到旋转的目的。具体怎么交换,我们可以通
阅读全文
摘要:【字符串与数组】Q:Write a method to replace all spaces in a string with ‘%20’题目:写一个算法将一个字符串中的空格替换成%20解答:很直观的解法,首先统计出字符串中空格个数,然后分配新的内存空间,依次从头到尾复制原字符串到新字符串中,遇到空格,则复制%20这三个字符。还有没有其他更好点的方法呢??char* replace(char* str){ int len=strlen(str); int spaceNum=0; int i; for(i=0;i<len;++i){ if(str[i]==' ') ++sp
阅读全文
摘要:【字符串与数组】Q:Write a method to decide if two strings are anagrams or not题目:写一个算法来判断两个字符串是否为换位字符串。(换位字符串是指组成字符串的字符相同,但位置不同)解答: 方法一:假设为ascii2码字符串,那么可以分配两个256大小的int数组,每个数组用于统计一个字符串各个字符出现的次数,最后,比较这两个int数组,看是否每个元素都相同。时间复杂度为O(n)。int anagrams1(char* str1,char* str2){ int len1=strlen(str1); int len2=strlen(str
阅读全文
摘要:【字符串与数组】Q: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 array is not FOLLOW UP Write the test cases for this method题目:设计一个算法,在不使用额外存储空间的情况下,去掉字符串中重复的字符。(注:允许使用一
阅读全文

浙公网安备 33010602011771号