长颈鹿Giraffe

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

07 2013 档案

摘要:C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是:TYPE b = (TYPE)a。C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用。const_cast,字面上理解就是去const属性。static_cast,命名上理解是静态类型转换。如int转换成char。dynamic_cast,命名上理解是动态类型转换。如子类和父类之间的多态类型转换。reinterpret_cast,仅仅重新解释类型,但没有进行二进制的转换。4种类型转换的格式,如:TYPE B = static_cast(TYPE)(a)。const_cast去掉类型的const或volat 阅读全文
posted @ 2013-07-30 17:00 长颈鹿Giraffe 阅读(273) 评论(0) 推荐(0)

摘要:这个问题来源于微博上看到的这样一道面试题:有一个待选国家的列表,以及国家的相对热门程度,请给出一个算法,随机选择一个国家,并且保证,越是热门的国家,随机选择它的可能性就越高。(1)如果待选国家列表中国家数量以及热门程度是确定的,例如总共有4个国家A,B,C,D,热度分别为1,2,5,2。那么我们只需要在A,B,B,C,C,C,C,C,D,D;中随机选择一个就行。使用随机函数获取一个1~N的随机数即可。(2)如果待选国家列表中国家数量以及热门程度是不确定的。那么就可以将这个问题规约为 蓄水池抽样 问题,问题定义可以简化如下:在不知道国家总数的情况下,如何从文件中随机的选择一个国家?蓄水池算法如前 阅读全文
posted @ 2013-07-29 17:42 长颈鹿Giraffe 阅读(475) 评论(2) 推荐(0)

摘要:Write code to partition a linked list around a value x, such that all nodes less than xcome before alt nodes greater than or equal to.分割一个链表,将值小于x的节点全部放在其他节点的前面。解答:我们可以创建两个不同的链表,其中一个用来连接比x小的节点,另外一个则连接大于等于x的节点,然后合并两个链表即可。public class Main { public static void appendToTail(List head, int d) { ... 阅读全文
posted @ 2013-07-24 12:12 长颈鹿Giraffe 阅读(495) 评论(0) 推荐(0)

摘要:Implement an algorithm to delete a node in the middle of a singly 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删除链表中的一个节点,但并没有给出链表的头结点,而是只能访问要删除的这 阅读全文
posted @ 2013-07-24 11:34 长颈鹿Giraffe 阅读(333) 评论(0) 推荐(0)

摘要:Implement an algorithm to find the kth to last element of a singly linked list.实现一个算法寻找链表中倒数第K个数..解答:关于链表的问题,书中也提到了关键的两个技巧,一个是递归;另一个是The"Runner"Technique,也就是使用两个指针同时遍历链表的方式。这道题可以分别用这两种方法来解决。import java.util.HashMap;public class List { int data; List next; public List(int d) { this.da... 阅读全文
posted @ 2013-07-24 11:07 长颈鹿Giraffe 阅读(314) 评论(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?译文:删除链表中的重复元素,另外,如果不使用临时存储空间怎么做?解答:如果空间没有限制,那么用一个map来标记链表中出现过的元素,然后从头到尾扫一遍链表,把出现过的删除就行了。时间复杂度O(n)。如果限制空间,那么可以设置两个指针n、m,当n指向某个元素时,m把该元素后面与它相同的元素删除, 时间复杂度O(n2) 阅读全文
posted @ 2013-07-23 11:52 长颈鹿Giraffe 阅读(307) 评论(0) 推荐(0)

摘要:第一:两个文件的交集,并集前提条件:每个文件中不得有重复行1. 取出两个文件的并集(重复的行只保留一份)2. 取出两个文件的交集(只留下同时存在于两个文件中的文件)3. 删除交集,留下其他的行1.cat file1 file2 | sort | uniq > file32. cat file1 file2 | sort | uniq -d > file33. cat file1 file2 | sort | uniq -u > file3第二:两个文件合并一个文件在上,一个文件在下cat file1 file2 > file3一个文件在左,一个文件在右paste fil 阅读全文
posted @ 2013-07-16 12:14 长颈鹿Giraffe 阅读(33285) 评论(0) 推荐(2)

摘要:insight是在Linux下一个比较好用的GDB的前端insight首页:http://sourceware.org/insight/index.php在这里下载源码:insight-6.8.tar.bz2,并解压tar jxvf insight-6.8a.tar.bz2进入源码目录,在编译之前要修改几个文件:1)修改insight-6.8/tk/generic/tk.h将(line 653)#define VirtualEvent (LASTEvent)#define ActivateNotify (LASTEvent + 1)#define DeactivateNotify (LASTE 阅读全文
posted @ 2013-07-14 00:35 长颈鹿Giraffe 阅读(1522) 评论(1) 推荐(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-12 17:49 长颈鹿Giraffe 阅读(247) 评论(0) 推荐(0)

摘要:原文Write an algorithm such that if an element in an MxN matrix is 0, its entire row andcolumn are set to 0.译文写一个算法,如果一个MxN中的一个元素为0,那么将这个元素所在的行和列都设置为0。解答遍历一次矩阵,当遇到元素等于0时,记录下这个元素对应的行和列。并设置一个标记。在遍历完之后,再根据之前的标记设置矩阵的值。import java.util.BitSet;public class Main { public static void setZero(int[][] mat, i... 阅读全文
posted @ 2013-07-12 17:35 长颈鹿Giraffe 阅读(315) 评论(0) 推荐(0)

摘要:原文:Given an image represented by an NxN matrix, where each pixel in the image is 4bytes, write a method to rotate the image by 90 degrees. Can you do this in place?译文给一个NxN的矩阵,写一个函数把矩阵旋转90度。 并且要求在原矩阵上进行操作,即不允许开辟额外的存储空间。解答(一)Hawstein在他的Blog中http://hawstein.com/posts/1.6.html介绍的方法是:可以分两步走。 第一步交换主对角线两侧 阅读全文
posted @ 2013-07-12 17:21 长颈鹿Giraffe 阅读(328) 评论(0) 推荐(0)

摘要:原文Implement a method to perform basic string compression using the countsof repeated characters. For example, the string aabcccccaaa would becomea2blc5a3. If the "compressed" string would not become smaller than the originalstring, your method should return the original string.译文利用计算字符个数的方 阅读全文
posted @ 2013-07-12 15:02 长颈鹿Giraffe 阅读(344) 评论(0) 推荐(0)

摘要:原文Write a method to replace all spaces in a string with'%20'. You may assume thatthe string has sufficient space at the end of the string to hold the additionalcharacters, and that you are given the "true" length of the string. (Note: if implementingin Java, please use a character 阅读全文
posted @ 2013-07-12 13:35 长颈鹿Giraffe 阅读(291) 评论(0) 推荐(0)

摘要:原文Given two strings, write a method to decide if one is a permutation of the other.译文给你两个字符串,写一个方法来判断其中一个是不是另一个的permutation。如果两个字符串含有相同的字符,仅仅是顺序可能不同,那么他们就叫permutations。例如"ABCDEF"和"FEDCBA",我们可以认为它们是permutation。解答如果长度不同,则一定不是,否则我们可以先将两个字符串内的字符按字典序排序,然后再判断是否相等。import java.util.Array 阅读全文
posted @ 2013-07-12 11:55 长颈鹿Giraffe 阅读(452) 评论(0) 推荐(0)

摘要:原文Implement a function void reverse(char* str) in C or C++ which reverses a null-terminatedstring.译文用C或者C++实现一个void reverse(char* str)的函数来逆转一个已null终止的字符串解答在C++里,设置俩指针,一个指向字符串头,一个指向尾,然后每次交换一个字符和,指针分别像中间移就行了。这里借用一下Hawstein(http://hawstein.com/posts/1.2.html)的代码好了:#include #include using namespace std; 阅读全文
posted @ 2013-07-12 11:33 长颈鹿Giraffe 阅读(361) 评论(0) 推荐(0)

摘要:原文:Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?译文:实现一个算法来判断一个字符串中的字符是否唯一(即没有重复).不能使用额外的数据结构。 (即只使用基本的数据结构)解答:假设题目中提到的字符是包含在ASCII中的字符的话,那么最多是256个。所以可以用一个256长的数组来标记,或者用一个8个32位的int值来标记。如果题目中没有规定只使用基本的数据结构的话,用BitSet来做也很方便的 阅读全文
posted @ 2013-07-12 11:09 长颈鹿Giraffe 阅读(305) 评论(0) 推荐(0)

摘要:1. Arrays and Strings1.1Hash Tables哈希表,简单的说就是由一个数组和一个hash函数组成实现key/value映射并且能高效的查找的数据结构。最简单的想法就是将hash(key)做为数组的下标(index)来存取。但是为了防止hash的冲突(collisions),数组的大小必须设置得足够大,因此上面这种简单的实现在实际中是不可取的。实际上,哈希表是一个固定大小的数组,数组的每个元素是一个链表(单向或双向)的头指针。每个元素被存放在hash(key)%array_length所在的链表中。如下图所示:另外,我们可以用二叉查找树(bst)来实现哈希表,在平衡树的 阅读全文
posted @ 2013-07-10 12:18 长颈鹿Giraffe 阅读(626) 评论(0) 推荐(0)