amazon题代码

55 篇文章 0 订阅
46 篇文章 2 订阅

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. int isRun(int num)  
  3. {  
  4.     int ret =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.     return ret;  
  18. }  
  19. int main()  
  20. {  
  21.     int year;  
  22.     scanf("%d",&year);  
  23.     if(isRun(year))  
  24.     {  
  25.         printf("run\n");  
  26.     }  
  27.     else  
  28.     {  
  29.             printf("no\n");  
  30.     }  
  31.     return 0;  
  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. void showAnother(char *str,int start,int num)  
  3. {  
  4.     int i=0;  
  5.     char c;  
  6.     if(str[start]=='\0')  
  7.     {  
  8.         printf("%s\n",str);  
  9.     }  
  10.   
  11.     if(str == NULL)  
  12.         return;  
  13.           
  14.     for(i=start;i<num;i++)  
  15.     {  
  16.         c = str[start];  
  17.         str[start]=str[i];  
  18.         str[i]=c;  
  19.         showAnother(str,start+1,num);  
  20.         c = str[start];  
  21.         str[start]=str[i];  
  22.         str[i]=c;  
  23.     }  
  24. }  
  25. void showDigit(char *str,int start,int num)  
  26. {  
  27.     int i=0;  
  28.     char c;  
  29.     if(str == NULL)  
  30.         return;  
  31.           
  32.     for(i=start;i<num;i++)  
  33.     {  
  34.         c = str[start];  
  35.         str[start]=str[i];  
  36.         str[i]=c;  
  37.         showAnother(str,start+1,num);  
  38.         c = str[start];  
  39.         str[start]=str[i];  
  40.         str[i]=c;  
  41.     }  
  42.   
  43.   
  44. }  
  45.   
  46.   
  47. int main()  
  48. {  
  49.     char str[]="122345";  
  50.   
  51.     showDigit(str,0,sizeof(str)/sizeof(str[0]));  
  52.   
  53.   
  54. }  

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

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


[cpp]  view plain copy
  1. #include "stdio.h"  
  2. #include "string.h"  
  3. #define MAX 1024  
  4. #define CHARLEN 256  
  5. void strReverse(char *str,int start,int len)  
  6. {  
  7.     int begin;  
  8.     int end;  
  9.     int temp,i;  
  10.     char c;  
  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. int main()  
  38. {  
  39.     char str[MAX];  
  40.     int i;  
  41.     int len;  
  42.     gets(str);  
  43.   
  44.     len = strlen(str);  
  45.     strReverse(str,0,len);  
  46.     printf("%s\n",str);  
  47.     return 0;  
  48. }  

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

[cpp]  view plain copy
  1. #include "stdio.h"  
  2. #include "string.h"  
  3. #define MAX 1024  
  4. #define CHARLEN 256  
  5. int main()  
  6. {  
  7.     char str[MAX];  
  8.     char strToMove[MAX];  
  9.     int mask[CHARLEN];  
  10.     int i;  
  11.     int len;  
  12.     int lenToMove;  
  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.   
  5. #define MAX 1024  
  6. #define CHARLEN 256  
  7.   
  8.   
  9.   
  10. int main()  
  11. {  
  12.     char str[MAX];  
  13.     int len;  
  14.     int number;  
  15.     int i,j;  
  16.     scanf("%s",str);  
  17.     len = strlen(str);  
  18.     number = pow(2,len);  
  19.     printf("All str list\n");  
  20.     for(i=1;i<number;i++)  
  21.     {  
  22.         for(j=0;j<len;j++)  
  23.         {  
  24.             if((i>>j)&1)  
  25.             {  
  26.                 putchar(str[j]);  
  27.             }  
  28.         }  
  29.         putchar('\n');  
  30.     }  
  31. }  


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

6. Swap two nodes in list

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

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

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

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


Find 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. #define MAX 1024  
  6. #define CHARLEN 256  
  7.   
  8. int Partion(char *str[MAX],int s,int e)  
  9. {  
  10.     int i=s-1;  
  11.     int j;  
  12.     char *p = str[e];  
  13.     char *temp;  
  14.     for(j=s;j<e;j++)  
  15.     {  
  16.         if(strcmp(str[j],p)<0)  
  17.         {  
  18.             temp = str[i+1];  
  19.             str[i+1] = str[j];  
  20.             str[j]=temp;  
  21.             i+=1;  
  22.         }  
  23.       
  24.     }  
  25.     temp = str[i+1];  
  26.     str[i+1] = p;  
  27.     str[e]=temp;  
  28.     return i+1;  
  29. }  
  30.   
  31. void quickSort(char *str[MAX],int start,int end)  
  32. {  
  33.     int q;  
  34.     if(start<end)  
  35.     {  
  36.         q = Partion(str,start,end);  
  37.         quickSort(str,start,q-1);  
  38.         quickSort(str,q+1,end);  
  39.       
  40.     }  
  41.   
  42. }  
  43. int comLen(char *str1,char *str2)  
  44. {  
  45.   
  46.     int count =0;  
  47.     assert(str1&&str2);  
  48.     while(*str1==*str2)  
  49.     {  
  50.         if(*str1=='\0'||*str2=='\0')  
  51.             break;  
  52.         count++;  
  53.         str1++;  
  54.         str2++;  
  55.     }  
  56.     return count;  
  57. }  
  58. int main()  
  59. {  
  60.     char str[MAX];  
  61.     char answer[MAX];  
  62.     char *strPoint[MAX];  
  63.     int i,len;  
  64.     int maxLen,clen;  
  65.     scanf("%s",str);  
  66.     len = strlen(str);  
  67.     for(i=0;i<len;i++)  
  68.     {  
  69.         strPoint[i]=str+i;  
  70.     }  
  71.     quickSort(strPoint,0,len-1);  
  72.     maxLen =0;  
  73.     for(i=0;i<len-1;i++)  
  74.     {  
  75.         clen = comLen(strPoint[i],strPoint[i+1]);  
  76.         if(clen >maxLen)  
  77.         {  
  78.             strncpy(answer,strPoint[i],clen);  
  79.             answer[clen]='\0';  
  80.             maxLen = clen;  
  81.         }  
  82.     }  
  83.     printf("%s\n",answer);  
  84. }  

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



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值