Cracking the coding interview
文章平均质量分 68
ddysher
这个作者很懒,什么都没留下…
展开
-
Top 150 Questions - 1.8
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 isSubstr原创 2012-04-23 23:13:22 · 884 阅读 · 0 评论 -
Top 150 Questions - 2.5
2.5 Given a circular linked list, implement an algorithm which returns node at the beginning of the loop. DEFINITION: Circular linked list: A (corrupt) linked list in which a node’s next pointer point原创 2012-04-30 16:07:46 · 1003 阅读 · 1 评论 -
Top 150 Questions - 2.4
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 func原创 2012-04-30 10:40:50 · 890 阅读 · 0 评论 -
Top 150 Questions - 1.7
1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.SetZero1()先扫描一次数组,记录下0元素的位置,然后进行设置。由于行列同时记录,那么同一行或列可能被设置多次,因此在SetZero2()中对行列分开存储。package原创 2012-04-16 12:38:44 · 823 阅读 · 0 评论 -
Top 150 Questions - 1.1
1.1 Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?这个题主要操作集中在查找上面,所以首先想到的就是哈希,时间复杂度为O(n);字符不超过256,定义256大小的数组就ok了。书上指出的其原创 2012-04-14 21:35:24 · 1060 阅读 · 0 评论 -
Top 150 Questions - 1.4
1.4 Write a method to decide if two strings are anagrams or not.这个题目最简单的方法应该就是排序了吧,排序偷懒了,直接用的api;另外一个就是计算每个字符出现的次数。package Question1_4;import java.util.Arrays;public class Question1_4{原创 2012-04-15 17:09:57 · 4064 阅读 · 1 评论 -
Top 150 Questions - 1.5
1.5 Write a method to replace all spaces in a string with ‘%20’.package Question1_5;public class Question1_5{ public static void main(String[] args) { Question1_5 q = new Question1_5();原创 2012-04-15 17:41:54 · 1104 阅读 · 0 评论 -
Top 150 Questions - 1.2
2.1 Write code to reverse a C-Style String. (C-String means that “abcd” is represented as five characters, including the null character.)C的字符串有段时间没用过了,出了几个问题。1. 在计算右边界时,需要记录s的位置,不能*s++;同时right要自减,否则原创 2012-04-14 22:42:42 · 981 阅读 · 0 评论 -
Top 150 Questions - 1.3
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原创 2012-04-15 15:58:50 · 1179 阅读 · 2 评论 -
Top 150 Questions - 2.3
2.3 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 r原创 2012-04-25 00:05:42 · 924 阅读 · 0 评论 -
Top 150 Questions - 2.2
2.2 Implement an algorithm to find the nth to last element of a singly linked list.两个指针间距n-1,后一个指针走到底时返回第一个指针即可,注意要检查cur是否为空,防止n过大。package Question2_2;public class Question2_2{ public stat原创 2012-04-24 23:38:01 · 838 阅读 · 0 评论 -
Top 150 Questions - 2.1
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?RemoveDuplicate1()使用了Java的Hashtable,如果发现了duplicate,原创 2012-04-24 22:52:58 · 1016 阅读 · 0 评论 -
Top 150 Questions - 1.6
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的时候以为要考移植性,不过既然用java原创 2012-04-16 10:37:18 · 1033 阅读 · 0 评论