cracking the code interview
文章平均质量分 54
bladeLight
这个作者很懒,什么都没留下…
展开
-
1.2
Write code to reverse a C-Style String. (C-String means that “abcd” is represented as five characters, including the null character.)void reverse(char* str){ if(str==NULL) return; int i=0,j=s原创 2013-07-29 22:46:30 · 503 阅读 · 0 评论 -
1.1
Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?1)使用附加结构:这里需要假定的是字符都是ASCII码(1个字节表表示),大小256的为扩展ASCII码(0-255),128为标准ASCI原创 2013-07-28 21:26:43 · 431 阅读 · 0 评论 -
2.5
Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.DEFINITIONCircular linked list: A (corrupt) linked list in which a node’s next pointer points t原创 2013-08-01 23:25:09 · 495 阅读 · 0 评论 -
2.1
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?分析:如何允许使用额外空间,我们可以使用map来记录已经存在的节点,遇到重复的节点就删除;如果不允许使用额外空原创 2013-07-31 22:08:49 · 500 阅读 · 0 评论 -
2.4
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 at the head of the list. Write a function原创 2013-08-01 00:03:37 · 492 阅读 · 0 评论 -
2.3
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 retu原创 2013-07-31 22:49:26 · 545 阅读 · 0 评论 -
2.2
Implement an algorithm to find the nth to last element of a singly linked list.分析:查找链表倒数第n个元素,这在剑指offer上有相同的习题Node* get_lastn(Node* head,int n){ if(head==NULL||n<=0) return NULL; Node* front=hea原创 2013-07-31 22:39:51 · 503 阅读 · 0 评论 -
1.8
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原创 2013-07-31 18:29:03 · 517 阅读 · 0 评论 -
1.6
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?分析:4个bytes告诉我们可以用整型来表示每个元素,关键是旋转原创 2013-07-30 14:23:07 · 788 阅读 · 0 评论 -
1.5
Write a method to replace all spaces in a string with ‘%20’.思路:耗费空间的解法是,再定义一个数组,将原数组复制到新数组中,复制的过程中进行替换,空间复杂度O(n); 优化:直接在原数组中操作,不过要从后往前,不然会造成覆盖。这让我想到了memcpy实现的过程中也可能出现覆盖需要从后往前覆盖的情况。时间复杂度:O(n)原创 2013-07-30 13:43:04 · 529 阅读 · 0 评论 -
1.4
Write a method to decide if two strings are anagrams or not.思路:使用一个count记录每个字符出现的次数,若出现的次数都相同,说明两个词是变位词bool isStringAnagrams(char s[],char t[]){ if(s==NULL||t==NULL||strlen(s)!=strlen(t))原创 2013-07-30 11:17:14 · 481 阅读 · 0 评论 -
1.3
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原创 2013-07-30 10:50:58 · 480 阅读 · 0 评论 -
Write a method that returns all subsets of a set
分析:1、每次向集合A中添加一个元素x,集合A为部分元素的所有组合 2、拷贝A,记为A1,将A1中每个元素和x进行拼接,然后将生成的元素放入A中void addAllOne(vector &to,vector &from){ for(int i=0;i<from.size();i++) to.push_back(from[i]);}void addAllTwo(vector >原创 2013-09-21 18:29:00 · 728 阅读 · 0 评论