长颈鹿Giraffe

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

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) 编辑

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