每天坚持Crack Code(Day 3)

来源:互联网 发布:js入门多久 编辑:程序博客网 时间:2024/06/06 03:37

问题:

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?

翻译:一张图像NxN矩阵,每个像素为4个字节,写一个方法将图像旋转90度,问可以原地处理不额外开辟新的存储空间?

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.

翻译:写一个方法,对于MxN矩阵,若有一个元素为0,则将其所在的行与列的所有元素置0。

Time complexity O(M*N) and space complexity O(M+N).

还有改进的空间?(马克一下,之后再改进)

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 isSubstring (i.e., “waterbottle” is a rotation of “erbottlewat”).

翻译:假设你有一个方法isSubstring是可以判断一个字符串是否是另一个的字串,如给定两个字符串,s1和s2,判断s2是否是s1的旋转字符串,只能调用一次isSubstring,(例如:waterbottle是erbottlewat的旋转字符串)

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?

翻译:在没有排序的链表中删除重复的项,并且不允许使用临时的缓存,如何解决这个问题?

1.如果可以使用额外的存储空间,我们就可以保存一个元素的出现情况,例如使用哈希表。c++标准库里有哈希表吗?不用哈希表用数组?数组的大小和元素的范围?