每天坚持Crack Code(Day 3)
2013-09-20 10:17
549 查看
问题:
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度,问可以原地处理不额外开辟新的存储空间?
void Swap(int &a, int &b)
{
//一个整形数异或同一个数字偶次数的值不变
a^=b;
b^=a;
a^=b;
}
//逆时针旋转90度,第一步做对角线上的对称变换,第二步做上下的对称变换。
//画个图可以比较清楚
void rotate(int matrix[][4],int n)
{
for(int i=0;i1.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。
void setZeros(int matrix[m]
,int m, int n)
{
bool row[4]={false};
bool colum[4]={false};
//Store the row and colum index with value 0
for(int i=0;iTime 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的旋转字符串)
bool isSubstring(string s1, string s2){
//s1.find(s2)返回s2在s1中的下标位置,如果没有找到,则返回特别的标志npos
if(s1.find(s2) != string::npos) return true;
else return false;
}
bool isRotation(string s1,string s2)
{
if(s1.length() != s2.length() || s1.length()<=0)
return false;
return isSubstring(s1+s1,s2);
}
int main()
{
string s1="apple";
string s2="pleap";
cout<
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++标准库里有哈希表吗?不用哈希表用数组?数组的大小和元素的范围?
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度,问可以原地处理不额外开辟新的存储空间?
void Swap(int &a, int &b)
{
//一个整形数异或同一个数字偶次数的值不变
a^=b;
b^=a;
a^=b;
}
//逆时针旋转90度,第一步做对角线上的对称变换,第二步做上下的对称变换。
//画个图可以比较清楚
void rotate(int matrix[][4],int n)
{
for(int i=0;i1.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。
void setZeros(int matrix[m]
,int m, int n)
{
bool row[4]={false};
bool colum[4]={false};
//Store the row and colum index with value 0
for(int i=0;iTime 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的旋转字符串)
bool isSubstring(string s1, string s2){
//s1.find(s2)返回s2在s1中的下标位置,如果没有找到,则返回特别的标志npos
if(s1.find(s2) != string::npos) return true;
else return false;
}
bool isRotation(string s1,string s2)
{
if(s1.length() != s2.length() || s1.length()<=0)
return false;
return isSubstring(s1+s1,s2);
}
int main()
{
string s1="apple";
string s2="pleap";
cout<
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++标准库里有哈希表吗?不用哈希表用数组?数组的大小和元素的范围?
相关文章推荐
- 每天坚持Crack Code(Day 1)
- 每天坚持Crack Code(Day 4)
- 每天坚持Crack Code(Day 5)
- 每天坚持Crack Code(Day 6)
- 每天坚持Crack Code(Day 2)
- 每天读一遍,坚持77天,你的英语就可以凑合跟米国人交流啦!(7day)
- 每天应该坚持写博客
- 如何提高意志力&如何坚持每天学习 来源: 杨只求的日志
- 如何坚持每天学习
- 写在淘宝店开张1周年,月销量从0k达到5k,每天努力多一点点,干什么事情都需要坚定坚持
- 每天一定要坚持学习至少半小时英语,否则昨天晚上就白失眠了.
- 【2016-10-28】【坚持学习】【Day15】【MongoDB】【初识】
- 坚持每天都在blog上写东西!
- 从今天起决定每天写篇BLOG,希望能坚持下去.
- 坚持每天发表一篇检讨日志(1)
- 坚持每天写一点jquery代码,慢慢的去理解
- 每天坚持10分钟,改变你的人生
- 新站SEOer,每天应该坚持的六件事
- 实现梦想,每天坚持30分钟
- 坚持,坚持每天学习,短时间的快速学习