[google面试CTCI] 1-6.图像旋转问题】的更多相关文章

[字符串与数组] 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: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 “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? 题目:编码实现从无序链表中移除重复项.          如果不能使用临时缓存,你怎么编码实现? 解答: 方法一:不使用额外的存储空间,直接在原始链表上进行操作.首先用一个指针指向链…
[链表] 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 || n<1) retu…
[链表] Q:Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node EXAMPLE Input: the node ‘c’ from the linked list a->b->c->d->e Result: nothing is returned, but the new linked list looks like…
创建链表.往链表中插入数据.删除数据等操作,以单链表为例. 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 i…
[字符串与数组] 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,咋看一下没问题,仔细一想不对啊,这样做后,很有可能操作完后,这个二维数…
[字符串与数组] 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…
[字符串与数组] Q:Write a method to decide if two strings are anagrams or not 题目:写一个算法来判断两个字符串是否为换位字符串.(换位字符串是指组成字符串的字符相同,但位置不同) 解答: 方法一:假设为ascii2码字符串,那么可以分配两个256大小的int数组,每个数组用于统计一个字符串各个字符出现的次数,最后,比较这两个int数组,看是否每个元素都相同.时间复杂度为O(n). int anagrams1(char* str1,c…
[字符串与数组] 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…