免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5442 | 回复: 4
打印 上一主题 下一主题

[算法] 【每日一题】今日面试题:熟悉的陌生人;及又见Google搜索之星分析 [复制链接]

论坛徽章:
7
2015年亚洲杯之约旦
日期:2015-03-05 17:03:522015亚冠之山东鲁能
日期:2015-09-29 13:01:2115-16赛季CBA联赛之四川
日期:2016-01-18 15:47:0215-16赛季CBA联赛之广夏
日期:2016-02-24 11:47:1515-16赛季CBA联赛之辽宁
日期:2016-11-01 09:45:4115-16赛季CBA联赛之青岛
日期:2017-02-15 10:02:182016科比退役纪念章
日期:2017-02-16 17:25:35
1 [报告]
发表于 2013-08-19 12:12 |只看该作者
赞楼主。。。中午研究研究。。。

论坛徽章:
0
2 [报告]
发表于 2013-08-30 15:37 |只看该作者
第二题的通用算法
  1. /**
  2. * 一个数组中有N个数字出现的次数都超过数组总长度L的1/(N+1),查找这些ID
  3. * */

  4. #include <iostream>
  5. #include <cstring>
  6. #include <map>
  7. #include <algorithm>
  8. #include <utility>
  9. using namespace std;

  10. //数组:arr
  11. //长度:size
  12. //数字个数:n
  13. map<int,int> find(int* arr,int size,size_t n)
  14. {
  15.         map<int,int> candidate;
  16.         for(int i=0;i<size;++i)
  17.         {
  18.                 if(candidate.find(arr[i])!=candidate.end())
  19.                 {
  20.                         ++candidate[arr[i]];
  21.                 }
  22.                 else if(candidate.size()==n)
  23.                 {
  24.                         for(auto index=candidate.begin();index!=candidate.end();)
  25.                         {
  26.                                 --(index->second);
  27.                                 if(index->second==0)
  28.                                         candidate.erase(index++); //注意这里的写法,否则,index已经失效,将没办法遍历
  29.                                 else
  30.                                         ++index;                 //注意区别
  31.                         }
  32.                 }
  33.                 else
  34.                 {
  35.                         candidate.insert(make_pair(arr[i],1));
  36.                 }
  37.         }
  38.         return candidate;
  39. }

  40. int main(int argc,char* argv[])
  41. {
  42.         int arr[]={1,2,3,5,1,2,3};
  43.         map<int,int> candidate=move(find(arr,sizeof(arr)/sizeof(int),3));
  44.         for_each(candidate.begin(),candidate.end(),
  45.                         [](const pair<int,int>& val)
  46.                         {
  47.                                 cout<<"Val: "<<val.first<<endl;
  48.                         }
  49.                         );
  50.         return 0;
  51. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2013-08-30 15:39 |只看该作者
第一题也很简单
就是判断是不是二分图
采用染色的方法
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP