amazon题代码

First Phone Interview:
1.提出尽可能多的方法使一个method可以返回多个不同type的值
2.reverse string
比如 "I have a dream" -> "dream a have I"
3.判断一个binary tree是不是对称的

Second Phone Interview:
1.给a list of number,返回前top K个(内存足够怎么做,内存不够怎么做)
2.用OOD来电梯
3.找两个链表的交集

Onsite 6轮 1轮HR 1轮午餐 4轮技术 (亚马逊web services team)
1.设计个电话本, 可以用那些数据结构
答案貌似可以用suffix tree或者hashtable
2.问research, OOD 交通灯系统
3.写函数算一个整数的阶层 n!, 又问了n很大,怎么办?
比如99%的n都在400000-900000之间,怎么提高函数的执行速度
4.给一个数组和一个数n,找出数组中的所有的对和等于n的
5.给手机键盘,给定某个按键序列比如'1489',返回这个按键序列生成的所有的正确单词

1.判断一年是否为闰年的方法

 

[cpp] view plain copy
 
  1. #include"stdio.h"
  2. intisRun(intnum)
  3. {
  4. intret=0;
  5. if(num%4==0)
  6. {
  7. ret=1;
  8. if(num%100==0)
  9. {
  10. ret=0;
  11. }
  12. if(num%400==0)
  13. {
  14. ret=1;
  15. }
  16. }
  17. returnret;
  18. }
  19. intmain()
  20. {
  21. intyear;
  22. scanf("%d",&year);
  23. if(isRun(year))
  24. {
  25. printf("run\n");
  26. }
  27. else
  28. {
  29. printf("no\n");
  30. }
  31. return0;
  32. }

2 Please write a program which can print all 6 digitsnumbers composed of 1, 2, 2,3,4,5.

 

 

[cpp] view plain copy
 
  1. #include"stdio.h"
  2. voidshowAnother(char*str,intstart,intnum)
  3. {
  4. inti=0;
  5. charc;
  6. if(str[start]=='\0')
  7. {
  8. printf("%s\n",str);
  9. }
  10. if(str==NULL)
  11. return;
  12. for(i=start;i<num;i++)
  13. {
  14. c=str[start];
  15. str[start]=str[i];
  16. str[i]=c;
  17. showAnother(str,start+1,num);
  18. c=str[start];
  19. str[start]=str[i];
  20. str[i]=c;
  21. }
  22. }
  23. voidshowDigit(char*str,intstart,intnum)
  24. {
  25. inti=0;
  26. charc;
  27. if(str==NULL)
  28. return;
  29. for(i=start;i<num;i++)
  30. {
  31. c=str[start];
  32. str[start]=str[i];
  33. str[i]=c;
  34. showAnother(str,start+1,num);
  35. c=str[start];
  36. str[start]=str[i];
  37. str[i]=c;
  38. }
  39. }
  40. intmain()
  41. {
  42. charstr[]="122345";
  43. showDigit(str,0,sizeof(str)/sizeof(str[0]));
  44. }

3 题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。

 

例如输入“I am astudent.”,则输出“student. a am I”。

 

[cpp] view plain copy
 
  1. #include"stdio.h"
  2. #include"string.h"
  3. #defineMAX1024
  4. #defineCHARLEN256
  5. voidstrReverse(char*str,intstart,intlen)
  6. {
  7. intbegin;
  8. intend;
  9. inttemp,i;
  10. charc;
  11. if(str==NULL)
  12. return;
  13. begin=start;
  14. for(end=start;end<=len;end++)
  15. {
  16. if(str[end]==''||str[end]=='\0')
  17. {
  18. temp=end;
  19. temp-=1;
  20. for(;begin<temp;begin++,temp--)
  21. {
  22. c=str[temp];
  23. str[temp]=str[begin];
  24. str[begin]=c;
  25. }
  26. begin=end+1;
  27. }
  28. }
  29. end=len-1;
  30. for(i=start;i<end;i++,end--)
  31. {
  32. c=str[i];
  33. str[i]=str[end];
  34. str[end]=c;
  35. }
  36. }
  37. intmain()
  38. {
  39. charstr[MAX];
  40. inti;
  41. intlen;
  42. gets(str);
  43. len=strlen(str);
  44. strReverse(str,0,len);
  45. printf("%s\n",str);
  46. return0;
  47. }

4 题目:输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。例如,输入”Theyare students.””aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.”

 

 

[cpp] view plain copy
 
  1. #include"stdio.h"
  2. #include"string.h"
  3. #defineMAX1024
  4. #defineCHARLEN256
  5. intmain()
  6. {
  7. charstr[MAX];
  8. charstrToMove[MAX];
  9. intmask[CHARLEN];
  10. inti;
  11. intlen;
  12. intlenToMove;
  13. memset(mask,0,sizeof(int)*CHARLEN);
  14. gets(str);
  15. getchar();
  16. scanf("%s",strToMove);
  17. len=strlen(str);
  18. lenToMove=strlen(strToMove);
  19. for(i=0;i<lenToMove;i++)
  20. {
  21. mask[strToMove[i]]=1;
  22. }
  23. for(i=0;i<len;i++)
  24. {
  25. if(!mask[str[i]])
  26. printf("%c",str[i]);
  27. }
  28. putchar('\n');
  29. }

5 求字符串所有子字符串(顺序不同算一个)

扫描二维码关注公众号,回复: 622846 查看本文章

题目大致意思要求一个字符串所有子字符串(长度从1到总长,但顺序不同,如ab和ba算一个)

此题有个解法可以利用二进制处理【只能是当字符串中不出现重复字符的时候】

 

 

[cpp] view plain copy
 
  1. #include"stdio.h"
  2. #include"string.h"
  3. #include"math.h"
  4. #defineMAX1024
  5. #defineCHARLEN256
  6. intmain()
  7. {
  8. charstr[MAX];
  9. intlen;
  10. intnumber;
  11. inti,j;
  12. scanf("%s",str);
  13. len=strlen(str);
  14. number=pow(2,len);
  15. printf("Allstrlist\n");
  16. for(i=1;i<number;i++)
  17. {
  18. for(j=0;j<len;j++)
  19. {
  20. if((i>>j)&1)
  21. {
  22. putchar(str[j]);
  23. }
  24. }
  25. putchar('\n');
  26. }
  27. }


 

当字符串重复的时候, 先排序nlogn,此时重复的字符会变成连续的,bcaeb 排序以后,abbce, 在用O(n)把排序数组去重,再用上面方法。

 

6. Swap two nodes in list

SWAP(node *head, node *first, node *second)

这道题目 还要构建节点比较复杂,题目的意思 就是交换两个节点。

当然这道题目 需要指针操作。

不能进行值的交换,因为类型不明哦。

 

7Find Longest Repeat SubString ,可以overlap,区分大小写

这道题一看就是典型的后缀数组的题目

应该比较简单

我跑一遍试试。

 

[cpp] view plain copy
 
  1. #include"stdio.h"
  2. #include"string.h"
  3. #include"math.h"
  4. #include"assert.h"
  5. #defineMAX1024
  6. #defineCHARLEN256
  7. intPartion(char*str[MAX],ints,inte)
  8. {
  9. inti=s-1;
  10. intj;
  11. char*p=str[e];
  12. char*temp;
  13. for(j=s;j<e;j++)
  14. {
  15. if(strcmp(str[j],p)<0)
  16. {
  17. temp=str[i+1];
  18. str[i+1]=str[j];
  19. str[j]=temp;
  20. i+=1;
  21. }
  22. }
  23. temp=str[i+1];
  24. str[i+1]=p;
  25. str[e]=temp;
  26. returni+1;
  27. }
  28. voidquickSort(char*str[MAX],intstart,intend)
  29. {
  30. intq;
  31. if(start<end)
  32. {
  33. q=Partion(str,start,end);
  34. quickSort(str,start,q-1);
  35. quickSort(str,q+1,end);
  36. }
  37. }
  38. intcomLen(char*str1,char*str2)
  39. {
  40. intcount=0;
  41. assert(str1&&str2);
  42. while(*str1==*str2)
  43. {
  44. if(*str1=='\0'||*str2=='\0')
  45. break;
  46. count++;
  47. str1++;
  48. str2++;
  49. }
  50. returncount;
  51. }
  52. intmain()
  53. {
  54. charstr[MAX];
  55. charanswer[MAX];
  56. char*strPoint[MAX];
  57. inti,len;
  58. intmaxLen,clen;
  59. scanf("%s",str);
  60. len=strlen(str);
  61. for(i=0;i<len;i++)
  62. {
  63. strPoint[i]=str+i;
  64. }
  65. quickSort(strPoint,0,len-1);
  66. maxLen=0;
  67. for(i=0;i<len-1;i++)
  68. {
  69. clen=comLen(strPoint[i],strPoint[i+1]);
  70. if(clen>maxLen)
  71. {
  72. strncpy(answer,strPoint[i],clen);
  73. answer[clen]='\0';
  74. maxLen=clen;
  75. }
  76. }
  77. printf("%s\n",answer);
  78. }

用快排+后缀数组来解决的,还好测试的比较顺利

 

 
 

First Phone Interview:
1.提出尽可能多的方法使一个method可以返回多个不同type的值
2.reverse string
比如 "I have a dream" -> "dream a have I"
3.判断一个binary tree是不是对称的

Second Phone Interview:
1.给a list of number,返回前top K个(内存足够怎么做,内存不够怎么做)
2.用OOD来电梯
3.找两个链表的交集

Onsite 6轮 1轮HR 1轮午餐 4轮技术 (亚马逊web services team)
1.设计个电话本, 可以用那些数据结构
答案貌似可以用suffix tree或者hashtable
2.问research, OOD 交通灯系统
3.写函数算一个整数的阶层 n!, 又问了n很大,怎么办?
比如99%的n都在400000-900000之间,怎么提高函数的执行速度
4.给一个数组和一个数n,找出数组中的所有的对和等于n的
5.给手机键盘,给定某个按键序列比如'1489',返回这个按键序列生成的所有的正确单词

1.判断一年是否为闰年的方法

 

[cpp] view plain copy
 
  1. #include"stdio.h"
  2. intisRun(intnum)
  3. {
  4. intret=0;
  5. if(num%4==0)
  6. {
  7. ret=1;
  8. if(num%100==0)
  9. {
  10. ret=0;
  11. }
  12. if(num%400==0)
  13. {
  14. ret=1;
  15. }
  16. }
  17. returnret;
  18. }
  19. intmain()
  20. {
  21. intyear;
  22. scanf("%d",&year);
  23. if(isRun(year))
  24. {
  25. printf("run\n");
  26. }
  27. else
  28. {
  29. printf("no\n");
  30. }
  31. return0;
  32. }


2 Please write a program which can print all 6 digitsnumbers composed of 1, 2, 2,3,4,5.

 

 

[cpp] view plain copy
 
  1. #include"stdio.h"
  2. voidshowAnother(char*str,intstart,intnum)
  3. {
  4. inti=0;
  5. charc;
  6. if(str[start]=='\0')
  7. {
  8. printf("%s\n",str);
  9. }
  10. if(str==NULL)
  11. return;
  12. for(i=start;i<num;i++)
  13. {
  14. c=str[start];
  15. str[start]=str[i];
  16. str[i]=c;
  17. showAnother(str,start+1,num);
  18. c=str[start];
  19. str[start]=str[i];
  20. str[i]=c;
  21. }
  22. }
  23. voidshowDigit(char*str,intstart,intnum)
  24. {
  25. inti=0;
  26. charc;
  27. if(str==NULL)
  28. return;
  29. for(i=start;i<num;i++)
  30. {
  31. c=str[start];
  32. str[start]=str[i];
  33. str[i]=c;
  34. showAnother(str,start+1,num);
  35. c=str[start];
  36. str[start]=str[i];
  37. str[i]=c;
  38. }
  39. }
  40. intmain()
  41. {
  42. charstr[]="122345";
  43. showDigit(str,0,sizeof(str)/sizeof(str[0]));
  44. }


3题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。

 

例如输入“I am astudent.”,则输出“student. a am I”。

 

[cpp] view plain copy
 
  1. #include"stdio.h"
  2. #include"string.h"
  3. #defineMAX1024
  4. #defineCHARLEN256
  5. voidstrReverse(char*str,intstart,intlen)
  6. {
  7. intbegin;
  8. intend;
  9. inttemp,i;
  10. charc;
  11. if(str==NULL)
  12. return;
  13. begin=start;
  14. for(end=start;end<=len;end++)
  15. {
  16. if(str[end]==''||str[end]=='\0')
  17. {
  18. temp=end;
  19. temp-=1;
  20. for(;begin<temp;begin++,temp--)
  21. {
  22. c=str[temp];
  23. str[temp]=str[begin];
  24. str[begin]=c;
  25. }
  26. begin=end+1;
  27. }
  28. }
  29. end=len-1;
  30. for(i=start;i<end;i++,end--)
  31. {
  32. c=str[i];
  33. str[i]=str[end];
  34. str[end]=c;
  35. }
  36. }
  37. intmain()
  38. {
  39. charstr[MAX];
  40. inti;
  41. intlen;
  42. gets(str);
  43. len=strlen(str);
  44. strReverse(str,0,len);
  45. printf("%s\n",str);
  46. return0;
  47. }


4题目:输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。例如,输入”Theyare students.””aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.”

 

 

[cpp] view plain copy
 
  1. #include"stdio.h"
  2. #include"string.h"
  3. #defineMAX1024
  4. #defineCHARLEN256
  5. intmain()
  6. {
  7. charstr[MAX];
  8. charstrToMove[MAX];
  9. intmask[CHARLEN];
  10. inti;
  11. intlen;
  12. intlenToMove;
  13. memset(mask,0,sizeof(int)*CHARLEN);
  14. gets(str);
  15. getchar();
  16. scanf("%s",strToMove);
  17. len=strlen(str);
  18. lenToMove=strlen(strToMove);
  19. for(i=0;i<lenToMove;i++)
  20. {
  21. mask[strToMove[i]]=1;
  22. }
  23. for(i=0;i<len;i++)
  24. {
  25. if(!mask[str[i]])
  26. printf("%c",str[i]);
  27. }
  28. putchar('\n');
  29. }

5 求字符串所有子字符串(顺序不同算一个)

题目大致意思要求一个字符串所有子字符串(长度从1到总长,但顺序不同,如ab和ba算一个)

此题有个解法可以利用二进制处理【只能是当字符串中不出现重复字符的时候】

 

 

[cpp] view plain copy
 
  1. #include"stdio.h"
  2. #include"string.h"
  3. #include"math.h"
  4. #defineMAX1024
  5. #defineCHARLEN256
  6. intmain()
  7. {
  8. charstr[MAX];
  9. intlen;
  10. intnumber;
  11. inti,j;
  12. scanf("%s",str);
  13. len=strlen(str);
  14. number=pow(2,len);
  15. printf("Allstrlist\n");
  16. for(i=1;i<number;i++)
  17. {
  18. for(j=0;j<len;j++)
  19. {
  20. if((i>>j)&1)
  21. {
  22. putchar(str[j]);
  23. }
  24. }
  25. putchar('\n');
  26. }
  27. }


 

当字符串重复的时候, 先排序nlogn,此时重复的字符会变成连续的,bcaeb 排序以后,abbce, 在用O(n)把排序数组去重,再用上面方法。

 

6. Swap two nodes in list

SWAP(node *head, node *first, node *second)

这道题目 还要构建节点比较复杂,题目的意思 就是交换两个节点。

当然这道题目 需要指针操作。

不能进行值的交换,因为类型不明哦。

 

7Find Longest Repeat SubString ,可以overlap,区分大小写

这道题一看就是典型的后缀数组的题目

应该比较简单

我跑一遍试试。

 

[cpp] view plain copy
 
  1. #include"stdio.h"
  2. #include"string.h"
  3. #include"math.h"
  4. #include"assert.h"
  5. #defineMAX1024
  6. #defineCHARLEN256
  7. intPartion(char*str[MAX],ints,inte)
  8. {
  9. inti=s-1;
  10. intj;
  11. char*p=str[e];
  12. char*temp;
  13. for(j=s;j<e;j++)
  14. {
  15. if(strcmp(str[j],p)<0)
  16. {
  17. temp=str[i+1];
  18. str[i+1]=str[j];
  19. str[j]=temp;
  20. i+=1;
  21. }
  22. }
  23. temp=str[i+1];
  24. str[i+1]=p;
  25. str[e]=temp;
  26. returni+1;
  27. }
  28. voidquickSort(char*str[MAX],intstart,intend)
  29. {
  30. intq;
  31. if(start<end)
  32. {
  33. q=Partion(str,start,end);
  34. quickSort(str,start,q-1);
  35. quickSort(str,q+1,end);
  36. }
  37. }
  38. intcomLen(char*str1,char*str2)
  39. {
  40. intcount=0;
  41. assert(str1&&str2);
  42. while(*str1==*str2)
  43. {
  44. if(*str1=='\0'||*str2=='\0')
  45. break;
  46. count++;
  47. str1++;
  48. str2++;
  49. }
  50. returncount;
  51. }
  52. intmain()
  53. {
  54. charstr[MAX];
  55. charanswer[MAX];
  56. char*strPoint[MAX];
  57. inti,len;
  58. intmaxLen,clen;
  59. scanf("%s",str);
  60. len=strlen(str);
  61. for(i=0;i<len;i++)
  62. {
  63. strPoint[i]=str+i;
  64. }
  65. quickSort(strPoint,0,len-1);
  66. maxLen=0;
  67. for(i=0;i<len-1;i++)
  68. {
  69. clen=comLen(strPoint[i],strPoint[i+1]);
  70. if(clen>maxLen)
  71. {
  72. strncpy(answer,strPoint[i],clen);
  73. answer[clen]='\0';
  74. maxLen=clen;
  75. }
  76. }
  77. printf("%s\n",answer);
  78. }


用快排+后缀数组来解决的,还好测试的比较顺利

 

猜你喜欢

转载自vergilwang.iteye.com/blog/2011226
今日推荐