长颈鹿Giraffe

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

2013年7月23日

摘要: 原文: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 阅读(303) 评论(0) 推荐(0) 编辑

2013年7月16日

摘要: 第一:两个文件的交集,并集前提条件:每个文件中不得有重复行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 阅读(33170) 评论(0) 推荐(2) 编辑

2013年7月14日

摘要: 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 阅读(1506) 评论(1) 推荐(0) 编辑

2013年7月12日

摘要: 原文: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 阅读(238) 评论(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 阅读(303) 评论(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 阅读(323) 评论(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 阅读(323) 评论(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 阅读(284) 评论(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 阅读(430) 评论(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 阅读(356) 评论(0) 推荐(0) 编辑