去哪儿---2015笔试编程题

去哪儿---2015笔试编程题

题目来源:http://s1.nowcoder.com/profile/7952866/test/8556622/25665

[编程题]二分查找

对于一个有序数组,我们通常采用二分查找的方式来定位某一元素,请编写二分查找的算法,在数组中查找指定元素。

给定一个整数数组A及它的大小n,同时给定要查找的元素val,请返回它在数组中的位置(从0开始),若不存在该元素,返回-1。若该元素出现多次,请返回第一次出现的位置。

测试样例:

[1,3,5,7,9],5,3

返回:1

 1 class BinarySearch 
 2 {
 3 public:
 4     int getPos(vector<int> A, int n, int val) 
 5     {
 6         // write code here
 7         if(n<=0) 
 8             return -1;
 9         int Mid=0,Left=0,Right=n-1;
10         while(Left<Right)
11         {
12             Mid = (Left+Right)/2;
13             if(A[Mid]>val)
14             {
15                 Right=Mid-1;
16             }
17             else if(A[Mid]<val)
18             {
19                 Left=Mid+1;
20             }
21             else
22             {
23                   
24                 Right=Mid;
25             }
26         }
27         if(A[Left]==val) 
28             return Left;
29         return -1;
30     }
31 };

[编程题]首个重复字符

对于一个字符串,请设计一个高效算法,找到第一次重复出现的字符。

给定一个字符串(不一定全为字母)A及它的长度n。请返回第一个重复出现的字符。保证字符串中有重复字符,字符串的长度小于等于500。

测试样例:

"qywyer23tdd",11

返回:y

 1 class FirstRepeat 
 2 {
 3 public:
 4     char findFirstRepeat(string A, int n) 
 5     {
 6         // write code here
 7         char ans;
 8         int hashTable[256] = {0};//数组统计每个字符出现的次数,赋初值为0
 9         for( int i = 0;i < n ;i++)
10         {
11             if(!hashTable[int(A[i])]) //hashTable[int(A[i])]不为0的话证明这已经是第二次出现,直接else ,break
12             {
13                hashTable[int(A[i])]++;
14             }        
15             else
16             {
17                 ans =  A[i];  
18                 break;            
19             }         
20         }
21         return ans; //返回结果A[i]即重复出现的字符
22 } 23 };

[编程题]寻找Coder

请设计一个高效算法,再给定的字符串数组中,找到包含"Coder"的字符串(不区分大小写),并将其作为一个新的数组返回。结果字符串的顺序按照"Coder"出现的次数递减排列,若两个串中"Coder"出现的次数相同,则保持他们在原数组中的位置关系。

给定一个字符串数组A和它的大小n,请返回结果数组。保证原数组大小小于等于300,其中每个串的长度小于等于200。同时保证一定存在包含coder的字符串。

测试样例:

["i am a coder","Coder Coder","Code"],3

返回:["Coder Coder","i am a coder"]

 1 class Coder {
 2 public:
 3     int counting(string &A){   //统计字符串中Coder出现的次数
 4         int c=0;
 5         int pos=A.find("Coder",0);
 6         while(pos!=string::npos){
 7             c++;
 8             if(pos+5<A.size())
 9                 pos=A.find("Coder",pos+5);
10             else
11                 break;
12         }
13         pos=A.find("coder",0);
14         while(pos!=string::npos){
15             c++;
16             if(pos+5<A.size())
17                 pos=A.find("coder",pos+5);
18             else
19                 break;
20         }
21        return c;
22     }
23     vector<string> findCoder(vector<string> A, int n) {
24         vector<string> result;
25         vector<int> res(n,0);
26         int maxval=0;
27         for(int i=0;i<n;i++){
28             res[i]=counting(A[i]);
29             if(res[i]>maxval)       
30                 maxval=res[i];     //出现次数的最大值
31         }
32         while(maxval>0){           //按照最大值到最小值逐渐存入到result中
33             for(int j=0;j<res.size();j++){
34                 if(res[j]==maxval)
35                     result.push_back(A[j]);
36             }
37             maxval--;
38         }
39           
40         return result;
41     }
42 };

 

posted @ 2017-06-04 22:06  walanwalan  阅读(428)  评论(0编辑  收藏  举报