随笔分类 -  Cracking the coding interview

摘要:题目原文:Describe how you could use a single array to implement three stacks.译文:你如何只用一个数组实现三个栈?解答我们可以很容易地用一个数组来实现一个栈,压栈就往数组里插入值,栈顶指针加1; 出栈就直接将栈顶指针减1;取栈顶值就把栈顶指针指向的单元的值返回; 判断是否为空就直接看栈顶指针是否为-1。如果要在一个数组里实现3个栈,可以将该数组分为3个部分。如果我们并不知道哪个栈将装 入更多的数据,就直接将这个数组平均分为3个部分,每个部分维护一个栈顶指针, 根据具体是对哪个栈进行操作,用栈顶指针去加上相应的偏移量即可。代码如 阅读全文
posted @ 2013-07-15 10:06 一枚程序员 阅读(426) 评论(0) 推荐(0) 编辑
摘要:题目原文: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 to an earlier node, so as to make a loop in the linked list.EXAMPLEInput: A –> B –> C –> D 阅读全文
posted @ 2013-07-11 11:23 一枚程序员 阅读(399) 评论(0) 推荐(0) 编辑
摘要:题目原文: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 that adds the two numbers and returns the sum as a linked list.EXAMPLEInput: (3 –> 1 –> 阅读全文
posted @ 2013-07-11 11:18 一枚程序员 阅读(230) 评论(0) 推荐(0) 编辑
摘要:题目原文: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->e Result: nothing is returned, but the new linked list looks like a->b->d->e译文:实现一个算法来删除单链表中间的一个结点,只给出指向 阅读全文
posted @ 2013-07-09 21:25 一枚程序员 阅读(322) 评论(0) 推荐(0) 编辑
摘要:题目原文:Implement an algorithm to find the nth to last element of a singly linked list.译文:实现一个算法从一个单链表中返回倒数第n个元素。解答思路一:这道题的考点在于我们怎么在一个单链表中找到倒数第n个元素? 由于是单链表,所以我们没办法从最后一个元素数起,然后数n个得到答案。 但这种最直观的思路显然是没错的,那我们有没有办法通过别的方式,从最后的元素数起数 n个来得到我们想要的答案呢。这个次序颠倒的思路可以让我们联想到一种数据结构:栈。我们如果遍历一遍单链表,将其中的元素压栈,然后再将元素一一出栈。那么, 第n 阅读全文
posted @ 2013-07-09 19:00 一枚程序员 阅读(281) 评论(0) 推荐(0) 编辑
摘要:题目原文:Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this problem if a temporary buffer is not allowed?译文:从一个未排序的链表中移除重复的项进一步地,如果不允许使用临时的缓存,你如何解决这个问题?解答如果可以使用额外的存储空间,我们就开一个数组来保存一个元素的出现情况。 对于这种情况,最好的解决方法当然是使用哈希表,但令人非常不爽的是C++标准里是没有 哈希表的(java里有)。网上有人用ext下的hash_ 阅读全文
posted @ 2013-07-09 15:38 一枚程序员 阅读(410) 评论(0) 推荐(0) 编辑
摘要:题目原文: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”).译文:假设你有一个isSubstring函数,可以检测一个字符串是否是另一个字符串 阅读全文
posted @ 2013-07-09 14:59 一枚程序员 阅读(411) 评论(0) 推荐(0) 编辑
摘要:题目原文: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时,记录下这个元素对应的行和列。 可以开一个行数组row和列数组col,当元素a[i][j]等于0时, 就把row[i]和col[j]置为true。第二次遍历矩阵时,当某个元素对应的行row[i] 或列col[j]被设置为true,说明该元素在需要 阅读全文
posted @ 2013-07-09 14:55 一枚程序员 阅读(196) 评论(0) 推荐(0) 编辑
摘要:题目原文: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的矩阵,图像中每个像素是4个字节,写一个函数把图像旋转90度。 你能原地进行操作吗?(即不开辟额外的存储空间)解答我们假设要将图像逆时针旋转90度,顺时针是一个道理。如果原图如下所示:1 2 3 4 5 6 7 8 9 10 11 12 1 阅读全文
posted @ 2013-07-09 14:53 一枚程序员 阅读(251) 评论(0) 推荐(0) 编辑
摘要:题目原文:Write a method to replace all spaces in a string with ‘%20’.译文:写一个函数,把字符串中所有的空格替换为%20 。解答简单题。先遍历一次字符串,得到空格个数,进而得到将空格转换成%20后的串长度 (每个空格替换为%20需要增加2个字符,x个空格增加2x个字符)。 然后从后向前依次对空格进行替换,非空格原样拷贝。如果原串有足够大的空间, 则替换过程直接在原串上进行, 因为从后向前替换的过程中,新串用到的空间一定是旧串不需要的空间,看图示:旧字符串:i am hawstein旧串索引: p新字符串:i%20am%20haws.. 阅读全文
posted @ 2013-07-09 14:48 一枚程序员 阅读(277) 评论(0) 推荐(0) 编辑
摘要:题目原文:Write a method to decide if two strings are anagrams or not.译文:写一个函数判断两个字符串是否是变位词。解答变位词(anagrams)指的是组成两个单词的字符相同,但位置不同的单词。比如说, abbcd和abcdb就是一对变位词。该题目有两种做法:O(nlogn)的解法由于组成变位词的字符是一模一样的,所以按照字典序排序后,两个字符串也就相等了。 因此我们可以用O(nlogn)的时间去排序,然后用O(n)的时间比较它们是否相等即可。代码如下:bool isAnagram1(string s, string t){ if... 阅读全文
posted @ 2013-07-09 14:43 一枚程序员 阅读(258) 评论(0) 推荐(0) 编辑
摘要:题目原文: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 UPWrite the test cases for this method.译文:设计算法并写出代码移除字符串中重复的字符,不能使用额外的缓存空间。注意: 可以使用额外的一 阅读全文
posted @ 2013-07-09 14:33 一枚程序员 阅读(353) 评论(0) 推荐(0) 编辑
摘要:题目原文:Write code to reverse a C-Style String. (C-String means that “abcd” is represented as five characters, including the null character.)译文:写代码翻转一个C风格的字符串。(C风格的意思是"abcd"需要用5个字符来表示,包含末尾的 结束字符)解答这道题如果就是要考察你有没有注意到C风格字符串最后的那个结束符,那我觉得还是像书 上写的那样,在代码中有所体现。代码如下:void reverse(char *s){ char *end = 阅读全文
posted @ 2013-07-09 11:34 一枚程序员 阅读(305) 评论(0) 推荐(0) 编辑
摘要:题目原文:Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?译文:实现一个算法来判断一个字符串中的字符是否唯一(即没有重复).不能使用额外的数据结构。 (即只使用基本的数据结构)解答首先,你可以问面试官,构成字符串的字符集有多大?是ASCII字符,还是只是26个字母? 还是有更大的字符集,对于不同的情况,我们可能会有不同的解决方案。如果我们假设字符集是ASCII字符,那么我们可以开一个大小为25 阅读全文
posted @ 2013-07-09 11:10 一枚程序员 阅读(407) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示